struct是值类型,隐式继承自System.ValueType,struct实例被分配在堆栈上,由系统内存回收;class是引用类型,class的实例被分配在托管堆上,由GC回收。
struct不可以有无参构造函数(这种说法不准确,ChuckLu 提醒应说成:不能包含显式的无参构造函数。也就是说,struct有默认的无参构造函数,不能显式声明),只能有带参构造函数;class当然可以有无参构造函数。
struct和class都可以通过new关键字创建实例。
struct不能用abstract修饰,可以在class前面加abstract关键字。
struct作为参数传递时,传递的是值,而class传递的是引用地址。
来看一个struct和class类型作为参数传递的例子。
class Program{static void Main(string[] args){PointStruct structPoint = new PointStruct();structPoint.x = 10;structPoint.y = 10;Console.WriteLine("struct的初始值是:x={0},y={1}",structPoint.x, structPoint.y);ChangeStructPoint(structPoint);Console.WriteLine("struct的初始值改变后是:x={0},y={1}", structPoint.x, structPoint.y);Console.WriteLine();PointClass pointClass = new PointClass(10, 10);Console.WriteLine("class的初始值是:x={0},y={1}", pointClass.x, pointClass.y);ChangeClassPoint(pointClass);Console.WriteLine("class的初始值改变后是:x={0},y={1}", pointClass.x, pointClass.y);Console.ReadKey();}static void ChangeStructPoint(PointStruct pointStruct){pointStruct.x = 20;pointStruct.y = 20;Console.WriteLine("正在改变struct的值是:x={0},y={1}", pointStruct.x, pointStruct.y);}static void ChangeClassPoint(PointClass pointClass){pointClass.x = 20;pointClass.y = 20;Console.WriteLine("正在改变class的值是:x={0},y={1}", pointClass.x, pointClass.y);}}public struct PointStruct{public int x;public int y;public PointStruct(int x, int y){this.x = x;this.y = y;}}public class PointClass{public int x;public int y;public PointClass(int x, int y){this.x = x;this.y = y;}}
结果是:
以上, struct和class的结构相似,当struct实例作为方法参数传递时,在堆栈上拷贝了一份传递给方法,在方法内改变其值,改变的是另一份struct实例的值(在ChangeStructPoint方法内部,按ChuckLu的理解,改变的是函数参数的值,对,就是这个意思);而class实例作为参数传递时,在堆栈上拷贝了一份传递给方法,堆栈上的2个实例指向同一个引用地址,改变托管堆上实例的值,也就相当于同时改变了堆栈上2个class实例的值。
总结:class完全可以替代struct能实现的所有功能,但,对于一些相对小的数据结构,应该考虑使用struct。
- 2楼ChuckLu
- 引用struct不可以有无参构造函数,只能有带参构造函数,说的不准确吧。,正确的说法:结构不能包含显式的无参构造函数,结构体是可以new的,应该是默认自带了无参构造函数
- Re: Darren Ji
- @ChuckLu,引用引用引用struct不可以有无参构造函数,只能有带参构造函数,说的不准确吧。,正确的说法:结构不能包含显式的无参构造函数,结构体是可以new的,应该是默认自带了无参构造函数,,你说的是对的,我在文章中加了说明。,,struct默认的无参构造函数负责给其中的字段初始化工作。,, class Program { static void Main(string[] args) { var s = new StructExample(); Console.WriteLine(s.x); //显示0 Console.WriteLine(s.y); //显示0 Console.WriteLine(string.IsNullOrEmpty(s.z) == true ? quot;空字符串quot; : s.z);//显示空字符串 Console.ReadKey();
- 1楼ChuckLu
- 此外,你的打印语句也不准确。,中间的改变值,你仅仅是对参数做操作。需要强调你改变的是函数参数的值。,,最后的结论,才涉及到值类型和引用类型