namespace structAndclass
{
class Program
{
static void Main(string[] args)
{
// 区别: struct 在传递的时候,传递是一个副本,并没有修改原始对象
// class 在传递的时候传递的是对原始对象的引用,修改原始对象的值
StructModifyPoint structModifyPoint = new StructModifyPoint();
structModifyPoint.y = 10;
structModifyPoint.x = 10;
Console.WriteLine("----------Struct--------");
// values are 10,10
Console.WriteLine("Initial values are :{0},{1}",structModifyPoint.x,structModifyPoint.y);
Console.WriteLine("Insurt structChangePoint():");
// values are 20,20
structChangePoint(structModifyPoint);
// values are 10,10
Console.WriteLine("After Modify,struct values are:{0},{1}", structModifyPoint.x, structModifyPoint.y);
ClassModifyPoint classModifyPoint = new ClassModifyPoint();
classModifyPoint.y = 10;
classModifyPoint.x = 10;
Console.WriteLine("----------Class--------");
// values are 10,10
Console.WriteLine("Initial values are :{0},{1}", classModifyPoint.x, classModifyPoint.y);
Console.WriteLine("Insurt classChangePoint():");
// values are 20,20
classChangePoint(classModifyPoint);
// values are 20,20
Console.WriteLine("After Modify,class values are:{0},{1}", classModifyPoint.x, classModifyPoint.y);
Console.ReadKey();
}
//struct
public static void structChangePoint(StructModifyPoint point)
{
point.x += 10;
point.y += 10;
Console.WriteLine("structChangePoint inner values are:{0},{1}",point.x,point.y);
}
//class
public static void classChangePoint(ClassModifyPoint point)
{
point.x += 10;
point.y += 10;
Console.WriteLine("classChangePoint inner values are:{0},{1}", point.x, point.y);
}
}
//结构体
public struct StructModifyPoint
{
public int x;
public int y;
public StructModifyPoint(int x,int y)
{
this.x = x;
this.y = y;
}
}
//类
public class ClassModifyPoint
{
public int x;
public int y;
public ClassModifyPoint()
{
}
public ClassModifyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
}