title: Memory address of an object in C#
body: <p>I have a function written some time ago (for .NET 3.5), and now that I have upgraded to 4.0</p>
  
  <p>I can't get it to work.</p>
  
  <p>The function is:</p>
  
  <pre><code>public static class MemoryAddress
  {
      public static string Get(object a)
      {
          GCHandle handle = GCHandle.Alloc(a, GCHandleType.Pinned);
          IntPtr pointer = GCHandle.ToIntPtr(handle);
          handle.Free();
          return "0x" + pointer.ToString("X");
      }
  }
  </code></pre>
  
  <p>Now, when I call it - MemoryAddress.Get(new Car("blue"))</p>
  
  <pre><code>public class Car
  {
      public string Color;
      public Car(string color)
      {
          Color = color;
      }
  }
  </code></pre>
  
  <p>I get the error: </p>
  
  <blockquote>
    <p>Object contains non-primitive or non-blittable data.</p>
  </blockquote>
  
  <p>Why doesn't it work anymore?</p>
  
  <p>How can I now get the memory address of managed objects?</p>