예제 #1
0
        public unsafe static void PointDemo()
        {
            int[] arry = null;
            arry = new int[10];
            fixed(int *pi = arry)
            {
                Console.WriteLine(@"array = 0x{0:x}", (int)pi);
            }

            int[] intArr = { 12, 13, 14, 15, 16 };
            for (int i = 0; i < intArr.Length; i++)
            {
                Console.WriteLine(@"array = 0x{0:x}", intArr[i]);
            }

            fixed(int *p = intArr)
            {
                Console.WriteLine((int)p);
            }

            CPoint pt = new CPoint();

            pt.X = 5;
            pt.Y = 6;
            // Pin pt in place:
            fixed(int *p = &pt.X)
            {
                SquareIntPointer(p);
            }

            fixed(int *p = &pt.X)
            {
                *p = 1;
            }

            double[] arr = { 0, 1.5, 2.3, 3.4, 4.0, 5.9 };
            fixed(int *p1 = &pt.X)
            {
                fixed(double *p2 = &arr[5])
                {
                    // Do something with p1 and p2.
                }
            }

            fixed(int *p1 = &pt.X)
            {
                fixed(double *p2 = &arr[5])
                {
                    // Do something with p1 and p2.
                }
            }
        }
예제 #2
0
        public static unsafe void UseAndPinPoint()
        {
            CPoint pt = new CPoint
            {
                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 now unpinned, and ready to be GC-ed once the method completes.
            Console.WriteLine(@"Point is: {0}", pt);
        }