title: pass c# struct by reference?
body: <p>In my c# application i receive pointer to c++ struct in callback/delegate. I'm not sure if <code>class</code> can do the trick but just casting c++ pointer to appropriate c# struct works fine, so I'm using c# struct for storing data.</p>
  
  <p>Now I want to pass reference to struct for further processing</p>
  
  <ul>
  <li>I can't use <code>class</code> because it probably will not "map" perfectly to c++ struct.</li>
  <li>I don't want to copy struct for better latency</li>
  </ul>
  
  <p>How can I do that?</p>
  
  <hr>
  
  <p>This example demonstrates that struct is passed by value, not by reference:</p>
  
  <pre><code>using System;
  
  namespace TestStruct
  {
      struct s
      {
          public int a;
      }
  
      class Program
      {
          static void Main(string[] args)
          {
              s s1 = new s
                         {
                             a = 1
                         };
              Foo(s1);
              Console.WriteLine("outer a = " + s1.a);
          }
  
          private static void Foo(s s1)
          {
              s1.a++;
              Console.WriteLine("inner a = " + s1.a);
          }
  
      }
  }
  </code></pre>
  
  <p>Output is:</p>
  
  <pre><code>inner a = 2
  outer a = 1
  </code></pre>