title: Why does my returned object not match my received object?
body: <p>I am trying to deserialize a byte array to a struct.</p>
  
  <p>Here is my deserialization function:</p>
  
  <pre><code>void RawDeserialize(byte[] bytearray, object obj)
  {
      int len = Marshal.SizeOf(obj);
  
      IntPtr i = Marshal.AllocHGlobal(len);
  
      Marshal.Copy(bytearray, 0, i, len);
  
      obj = Marshal.PtrToStructure(i, obj.GetType());
  
      Marshal.FreeHGlobal(i);
  }
  </code></pre>
  
  <p>I call it with</p>
  
  <pre><code>RawDeserialize(outarr, outbuf);
  </code></pre>
  
  <p>Where outarr is a byte array of length 22 and outbuf is my struct which looks like this:</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential,Size =22)]
  public struct ID_OUTPUT
  {
      public HEADER_OUTPUT hdr; //Another struct size=8
      public byte bType;
      public byte bRunning;
      [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 8)]
      public string softwareName; 
      public short softwareVersion;
  } 
  </code></pre>
  
  <p>When I step-through debug in my deserialization function, <em>obj</em> is filled with correct values, but on return <em>outbuf</em> is filled with zeroes (or is never assigned to because I originally initialize everything to zero). </p>
  
  <p>My initial thought is the object is not being passed by reference, but I assumed this should work because I found this deserialization function on another SO question (which I do not have the link for anymore).</p>
  
  <p>So then I try to use the <em>ref</em> keyword, but then I get an error <em>cannot convert from ref ID_OUTPUT to ref object</em>.</p>