欢迎来到天天文库
浏览记录
ID:33658280
大小:502.26 KB
页数:13页
时间:2019-02-28
《c#传值调用和传引用调用》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、传值调用和传引用调用是几乎所有主流语言都会涉及到的问题,下面我谈谈我对C#中传值调用和传引用调用的理解。1.一般对C#中传值调用和传引用调用的理解·如果传递的参数是基元类型(int,float等)或结构体(struct),那么就是传值调用。·如果传递的参数是类(class)那么就是传引用调用。·如果传递的参数前有ref或者out关键字,那么就是传引用调用。验证示例的代码如下:?1234567891011121314151617181920usingSystem;publicclassArgsByRefOr
2、Value{publicstaticvoidMain(string[]args){//实验1.传值调用--基元类型inti=10;Console.WriteLine("beforecallChangeByInt:i="+i.ToString());ChangeByInt(i);Console.WriteLine("aftercallChangeByInt:i="+i.ToString());Console.WriteLine("=======================================
3、=======");//实验2.传值调用--结构体Person_valp_val=newPerson_val();p_val.name="oldvalname";Console.WriteLine("beforecallChangeByStruct:p_val.name="+p_val.name);ChangeByStruct(p_val);Console.WriteLine("aftercallChangeByStruct:p_val.name="+p_val.name);Console.WriteLi
4、ne("==============================================");//实验3.传引用调用--类Person_refp_ref=newPerson_ref();p_ref.name="oldrefname";Console.WriteLine("beforecallChangeByClass:p_ref.name="+p_ref.name);ChangeByClass(p_ref);Console.WriteLine("aftercallChangeByClass:p
5、_ref.name="+p_ref.name);Console.WriteLine("=============================================="21222324252627282930313233343536373839404142);//实验4.传引用调用--利用refPerson_refp=newPerson_ref();p.name="oldrefname";Console.WriteLine("beforecallChangeByClassRef:p.name=
6、"+p.name);ChangeByClassRef(refp);Console.WriteLine("aftercallChangeByClassRef:p.name="+p.name);Console.ReadKey(true);}staticvoidChangeByInt(inti){i=i+10;Console.WriteLine("whencallingChangeByInt:i="+i.ToString());}staticvoidChangeByStruct(Person_valp_val)
7、{p_val.name="newvalname";Console.WriteLine("whencallingChangeByStruct:p_val.name="+p_val.name);}staticvoidChangeByClass(Person_refp_ref){p_ref.name="newrefname";Console.WriteLine("whencallingChangeByClass:p_ref.name="+p_ref.name);}staticvoidChangeByClassR
8、ef(refPerson_refp){p.name="newrefname";Console.WriteLine("whencallingChangeByClassRef:p.name="+p.name);}}publicstructPerson_val{publicstringname;}publicclassPerson_ref{publicstringname;}43444546474849505152535455565
此文档下载收益归作者所有