在使用类对象和函数使用时,使用的是引用传递,所以字段改变
在使用结构对象和函数使用时,是用的是值传递,所以字段没有改变
程序:
- using System; class class_wsy
- { public int x;
- } struct struct_wsy
- { public int x;
- } class program
- { public static void class_t(class_wsy obj)
- { obj.x = 90;
- } public static void struct_t(struct_wsy obj)
- { obj.x = 90;
- } public static void Main()
- { class_wsy obj_1 = new class_wsy();
- struct_wsy obj_2 = new struct_wsy(); obj_1.x = 100;
- obj_2.x = 100; class_t(obj_1);
- struct_t(obj_2); Console.WriteLine("class_wsy obj_1.x={0}",obj_1.x);
- Console.WriteLine("struct_wsy obj_2.x={0}",obj_2.x); Console.Read();
- } }
结果为:
- class_wsy obj_1.x=90 struct_wsy obj_2.x=100
|
|










