classProgram{staticvoidMain(string[] args){Student stu =newStudent();int y =100;
stu.AddOne(y);//输出为101
Console.WriteLine(y);//输出为100//传进来的值在方法体内部会有一个副本,改变的是副本的值(x=x+1),它不会影响方法体外面的值(y=100)}}classStudent{publicvoidAddOne(int x)//x:声明时不带任何修饰符,符合值参数定义//int:Int32是结构体数据类型,结构体数据类型属于值类型{
x = x +1;
Console.WriteLine(x);}}
引用类型:
classProgram{staticvoidMain(string[] args){Student stu =newStudent(){ Name ="Tim"};SomeMethod(stu);//输出为Tom//参数"Tim"传进去后,SomeMethod方法为参数赋了新值,这个新值是//一个新对象在堆内存当中的地址,而这个新对象的Name属性是"Tom",//所以打印出来是Tom。
Console.WriteLine(stu.Name);//输出为Tim//方法外部所引用的实例并没有改变。}staticvoidSomeMethod(Student stu){
stu =newStudent(){ Name ="Tom"};//新对象,新值
Console.WriteLine(stu.Name);}}classStudent{publicstring Name {get;set;}}
classProgram{staticvoidMain(string[] args){Student stu =newStudent(){ Name ="Tim"};UpdateObject(stu);
Console.WriteLine("HashCode={0},Name={1}", stu.GetHashCode(), stu.Name);//输出结果相同}staticvoidUpdateObject(Student stu){
stu.Name ="Tom";
Console.WriteLine("HashCode={0},Name={1}",stu.GetHashCode(),stu.Name);}}classStudent{publicstring Name {get;set;}}