在C#中,方法的参数传递有四种类型:传值(byvalue),传址(byreference),输出参数(byoutput),数组参数(byarray)。传值参数无需额外的修饰符,传址参数需要修饰符ref,输出参数需要修饰符out,数组参数需要修饰符params。传值参数在方法调用过程中如果改变了参数的值,那么传入方法的参数在方法调用完成以后并不因此而改变,而是保留原来传入时的值。传址参数恰恰相反,如果方法调用过程改变了参数的值,那么传入方法的参数在调用完成以后也随之改变。实际上从名称上我们可以清楚地看出两者的含义——传值参数传递的是调用参数的一份拷贝,而传址参数传递的是调用参数的内存地址,该参数在方法内外指向的是同一个存储位置。下面总结几种在项目中常用的参数传递:1、常规值类型参数传递(如:整型参数传递)usingSystem;classProgram{///summary///无引用参数////summary///paramname=m/param///paramname=n/paramstaticvoidParamTest(intm,intn){m=1;n=2;Console.WriteLine(now,m={0},n={1},m,n);}///summary///out参数////summary///paramname=m/param///paramname=n/paramstaticvoidOutTest(outintm,outintn){//离开这个函数前,必须对m和n赋值,否则会报错。//n=m;//上面这行会报错,因为使用了out后,m和n都清空了,需要重新赋值,即使调用函数前赋过值也不行m=1;n=2;}///summary///ref参数////summary///paramname=m/param///paramname=n/paramstaticvoidRefTest(refintm,refintn){m=1;n=m;}publicstaticvoidMain(){//值类型传递参数inti=22,j=33;ParamTest(i,j);Console.WriteLine(still,i={0};j={1},i,j);//outtestintx,y;//out使用前,变量可以不赋值OutTest(outx,outy);Console.WriteLine(herecomestheoutparameter:);Console.WriteLine(x={0};y={1},x,y);intxx=11,yy=22;OutTest(outxx,outyy);Console.WriteLine(herecomestheoutparameter:);Console.WriteLine(xx={0};yy={1},xx,yy);//reftestinta,b;//RefTest(refa,refb);//上面这行会出错,ref使用前,变量必须赋值intaa=10,bb=100;RefTest(refaa,refbb);Console.WriteLine(herecomestherefparameter:);Console.WriteLine(aa={0};bb={1},aa,bb);}}2、结构参数传递usingSystem;publicstructStudent{publicstringname;publicStudent(stringname){this.name=name;}}classProgram{///summary///无引用参数////summary///paramname=stu/paramstaticvoidParamTest(Studentstu){stu.name=jefferyzhao;Console.WriteLine(now,thestudent'snameis{0},stu.name);}///summary///out参数////summary///paramname=m/param///paramname=n/paramstaticvoidOutTest(outStudentstu){stu.name=jefferyzhao;}///summary///ref参数////summary///paramname=m/param///paramname=n/paramstaticvoidRefTest(refStudentstu){stu.name=jefferyzhao;}publicstaticvoidMain(){//结构类型传递参数Studentstu=newStudent(jeffwong);ParamTest(stu);Console.WriteLine(still,student'sname:{0},stu.name);//outtestStudentoutStu;//out使用前,变量可以不赋值OutTest(outoutStu);Console.WriteLine(herecomestheoutparameter:);Console.WriteLine(student'sname:{0},outStu.name);StudentoutStu1=newStudent(jeffwong);OutTest(outoutStu1);Console.WriteLine(herecomestheoutparameter:);Console.WriteLine(student'sname:{0},outStu1.name);//reftest//StudentrefStu;//RefTest(refrefStu);//上面这行会出错,ref使用前,变量必须赋值StudentrefStu1=newStudent(jeffwong);RefTest(refrefStu1);Console.WriteLine(herecomestherefparameter:);Console.WriteLine(student'sname:{0},refStu1.name);}}3、普通引用类型参数传递usingSystem;publicclassStudent{publicstringname;publicStudent(stringname){this.name=name;}}classProgram{staticvoidParamTest(Studentstu){Console.WriteLine(beforemodified,thestudent'snameis{0},stu.name);stu.name=jefferyzhao;Console.WriteLine(now,thestudent'snameis{0},stu.name);}///summary///out参数////summary///paramname=m/param///paramname=n/paramstaticvoidOutTest(outStudentstu){stu=newStudent();//必须初始化,否则编译错误stu.name=jefferyzhao;}///summary///ref参数////summary///paramname=m/param///paramname=n/paramstaticvoidRefTest(refStudentstu){stu.name=jefferyzhao;}publicstaticvoidMain(){//引用类型传递参数Studentstu=newStudent(jeffwong);ParamTest(stu);Console.WriteLine(still,student'sname:{0},stu.name);//outtestStudentoutStu=newStudent(jeffwong);//out使用前,变量可以不赋值OutTest(outoutStu);Console.WriteLine(herecomestheoutparameter:);Console.WriteLine(student'sname:{0},outStu.name);//reftest//StudentrefStu;//RefTest(refrefStu);//上面这行会出错,ref使用前,变量必须赋值StudentrefStu1=newStudent(jeffwong);RefTest(refrefStu1);Console.WriteLine(herecomestherefparameter:);Console.WriteLine(student'sname:{0},refStu1.name);}}4、string类型参数传递(****)usingSystem;classProgram{///summary///无引用参数////summary///paramname=m/param///paramname=n/paramstaticvoidParamTest(stringm,stringn){m=1;n=2;Console.WriteLine(now,m={0},n={1},m,n);}///summary///out参数////summary///paramname=m/param///paramname=n/paramstaticvoidOutTest(outstringm,outstringn){//离开这个函数前,必须对m和n赋值,否则会报错。//n=m;//上面这行会报错,因为使用了out后,m和n都清空了,需要重新赋值,即使调用函数前赋过值也不行m=1;n=2;}///summary///ref参数////summary///paramname=m/param///paramname=n/paramstaticvoidRefTest(refstringm,refstringn){m=1;n=m;}publicstaticvoidMain(){//string类型传递参数stringi=22,j=33;ParamTest(i,j);Console.WriteLine(still,i={0};j={1},i,j);//outteststringx,y;//out使用前,变量可以不赋值OutTest(outx,outy);Console.WriteLine(herecomestheoutparameter:);Console.WriteLine(x={0};y={1},x,y);stringxx=11,yy=22;OutTest(outxx,outyy);Console.WriteLine(herecomestheoutparameter:);Console.WriteLine(xx={0};yy={1},xx,yy);//refteststringa,b;//RefTest(refa,refb);//上面这行会出错,ref使用前,变量必须赋值stringaa=10,bb=100;RefTest(refaa,refbb);Console.WriteLine(herecomestherefparameter:);Console.WriteLine(aa={0};bb={1},aa,bb);}}通过上面的代码,我们发现string类型参数传递返回的结果和常规值类型参数传递(如:整型参数传递)返回的结果一致,这里把string类型单独拿出来,是为了区别对待string这种特殊类型。本质上string是引用类型,但是在实际计算和处理的时候,它处处透漏出值类型的特征。关于string类型,网上有无数精彩的文章介绍,