unsafe public static void UseAndPinPoint() { PointRef pt = new PointRef(); pt.x = 5; pt.y = 6; // pin pt in place so it will not // be moved or GC-ed. fixed(int *p = &pt.x) { // Use int* variable here! } // pt is now unpinned, and ready to be GC-ed. Console.WriteLine("Point is: {0}", pt); }
public static unsafe void UseAndPinPoint() { PointRef pt = new PointRef(); pt.x = 5; pt.y = 6; // pin pt in place so it will not // be moved or GC-ed. fixed (int* p = &pt.x) { // Use int* variable here! } // pt is now unpinned, and ready to be GC-ed once // the method completes. Console.WriteLine("Point is: {0}", pt); }
public unsafe static void UseAndPinPoint() { PointRef pt = new PointRef { x = 5, y = 6 }; // Pin pt in place so it will not // be moved or GC-ed. fixed(int *p = &pt.x) { // Use int* variable here! } // pt is not unpinned, and ready to be GC-ed one // the method completes. Console.WriteLine("Point is: {0}", pt); }
unsafe static void UseAndPinPoint() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("=> Pinning a Type via the fixed Keyword"); PointRef pt = new PointRef(); pt.x = 5; pt.y = 6; //Pin pt in place so it will not be moved or GC-ed. fixed(int *p = &pt.x) { // Use int* variable here. } //pt is now unpinned. Console.WriteLine($"Point is: {pt}"); }
unsafe static void UseAndPinPointRef() { PointRef pt = new PointRef(); pt.x = 5; pt.y = 6; //ERROR int* p = &pt.x; fixed (int* p = &pt.x) { Console.WriteLine("p: {0:X}", (int)p); } //fixed (PointRef* pPref = &pt) { ERROR // Console.WriteLine("p: {0:X}", (int)p); //} }
unsafe public static void UseAndPinPoint() { PointRef pt = new PointRef(); pt.x = 5; pt.y = 6; fixed (int* p = &pt.x) { *p = 12; } Console.WriteLine("Point is: {0}", pt); }