C#中值传递与引用传递的区别 .

以值传递参数

当实参当作值来传递时,就产生了一个新的拷贝。

[c-sharp] view plain copy print ?
  1. class Test  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            int x=8;  
  6.            Fo(x);  
  7.            Console.WriteLine("x={0}", x);  
  8.        }  
  9.       
  10.        static void Fo(int p)  
  11.        {  
  12.            p = p + 1;  
  13.            Console.WriteLine("p={0}", p);  
  14.        }  
  15.    }  

程序运行结果为:p=9,x=8;即X的值不会受P影响,给P赋一个新值并不会改变X的内容,因为P和X存在于内存中不同的位置。

 

同理,用传值的方式传递一个引用类型对象时,只是复制这个对象本身,即复制其地址值,而不是它指代的对象。下面代码中Fo中看到的StringBuilder对象,就是在Main方法中实例化的那一个,只是有不同的引用指向它而已。

[c-sharp] view plain copy print ?
  1. static void Fo(StringBuilder foSB)  
  2.         {  
  3.             foSB.Append("test");  
  4.             foSB = null;  
  5.         }  
  6.         static void Main()  
  7.         {  
  8.             StringBuilder sb = new StringBuilder();  
  9.             Fo(sb);  
  10.             Console.WriteLine(sb.ToString());  
  11.         }  

运行结果:test.

换句话说,sb和foSB是指向同一对象的不同引用变量。因为foSB是引用的拷贝,把它置为null并没有把sb置为 null.

 

ref修饰符

当使用rel关键字时,表示是用引用的方式传递参数。实参和形参引用的都是同一个对象,改变其中一个的引用值,另一个也会改变。

[c-sharp] view plain copy print ?
  1. static void Main(string[] args)  
  2.        {  
  3.            int x = 8;  
  4.            Fo(ref x);  
  5.            Console.WriteLine("x={0}", x);  
  6.        }  
  7.   
  8.        static void Fo(ref int p)  
  9.        {  
  10.             
  11.            p = p + 1;  
  12.            Console.WriteLine("p={0}", p);  
  13.        }  
 

运行结果:P=9;X=9  。  

如果在函数FO中改变P的值,则X的值也会随之改变。

[c-sharp] view plain copy print ?
  1. static void Main(string[] args)  
  2.         {  
  3.             int x = 8;  
  4.             Fo(ref x);  
  5.             Console.WriteLine("x={0}", x);  
  6.         }  
  7.   
  8.         static void Fo(ref int p)  
  9.         {  
  10.             p = 10;  
  11.             p = p + 1;  
  12.             Console.WriteLine("p={0}", p);  
  13.         }  

运行结果:P=11,X=11;

ref修饰符在写函数和调用函数时都一定要出现。

ref修饰符主要应该于实现交换的方法中。

[c-sharp] view plain copy print ?
  1. static void Swap(ref string a, ref string b)  
  2.         {  
  3.             string temp = a;  
  4.             a = b;  
  5.             b = temp;  
  6.         }  
  7.         static void Main()  
  8.         {  
  9.             string x = "Hello";  
  10.             string y = "World";  
  11.             Swap(ref x, ref y);  
  12.             Console .WriteLine (x);  
  13.             Console.WriteLine(y);  
  14.   
  15.         }  

运行结果:World Hello

 

out修饰符

out修饰符与ref修饰符非常相似,除了以下两点为:

一,在调用函数时不需要赋值。

二,在函数退出前必须赋值。

out修饰符通常用于需要从方法中获取多个返回值的时候

[c-sharp] view plain copy print ?
  1. static void Split(string name, out string firstName, out string lastName)  
  2.         {  
  3.             int i=name .LastIndexOf (' ');  
  4.             firstName =name .Substring (0,i);  
  5.             lastName =name .Substring (i+1);  
  6.         }  
  7.         static void Main()  
  8.         {  
  9.             string name = "Steven Francis";  
  10.             string a, b;  
  11.             Split(name, out a, out b);  
  12.             Console .WriteLine (a);  
  13.             Console .WriteLine (b);  
  14.         }  

运行结果:a=Steven,b=Francis

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值