using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using System.Dynamic; using System.ComponentModel; using System.Diagnostics; namespace ConsoleApplication { public struct MyStruct { public int a; public int b; } class Program { static unsafe void Main(string[] args) { MyStruct ms = new MyStruct(); //获取MyStruct的首地址 Console.WriteLine((uint)&ms); //获取值类型的大小 Console.WriteLine(sizeof(MyStruct)); //在堆栈上分配内存空间 int* p = stackalloc int[10]; *p = 100; Console.WriteLine("*p={0}", *p); Console.ReadLine(); } } }
C#编译器提供了/unsafe开关来控制是否允许对unsafe关键字的代码进行编译