private static void MyPassByValueMethod(Blittable x) { //x = null; x.Data = 5; x = new Blittable(){ Data = 0 }; GCHandle handle = GCHandle.Alloc(x, GCHandleType.Pinned); IntPtr address = handle.AddrOfPinnedObject(); Console.WriteLine(address.ToString("x") + " <- MyPassByValueMethod. Value: " + x.Data); handle.Free(); }
private static void TestPassByReference() { Blittable blit = new Blittable() { Data = 100, }; GCHandle handle = GCHandle.Alloc(blit, GCHandleType.Pinned); IntPtr address = handle.AddrOfPinnedObject(); Console.WriteLine(address.ToString("x") + " <- Before pass by value. Value: " + blit.Data); MyPassByValueMethod(blit); address = handle.AddrOfPinnedObject(); Console.WriteLine(address.ToString("x") + " <- After pass by value. Value: " + blit.Data); MyPassByReferenceMethod(ref blit); address = handle.AddrOfPinnedObject(); Console.WriteLine(address.ToString("x") + " <- After pass by ref. Value: " + blit.Data); handle.Free(); }
private static void MyPassByReferenceMethod(ref Blittable y) { //y = null; //y.Data = 2; y = new Blittable() { Data = 345, }; GCHandle handle = GCHandle.Alloc(y, GCHandleType.Pinned); IntPtr address = handle.AddrOfPinnedObject(); Console.WriteLine(address.ToString("x") + " <- MyPassByReferenceMethod. Value: " + y.Data); handle.Free(); }