struct是值类型,隐式继承自System.ValueType,struct实例被分配在堆栈上,由系统内存回收;class是引用类型,class的实例被分配在托管堆上,由GC回收。
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实例的值;而class实例作为参数传递时,在堆栈上拷贝了一份传递给方法,堆栈上的2个实例指向同一个引用地址,改变托管堆上实例的值,也就相当于同时改变了堆栈上2个class实例的值。
总结:class完全可以替代struct能实现的所有功能,但,对于一些相对小的数据结构,应该考虑使用struct。
原标题:如何选择使用结构或类
关键词: