C#学习:函数的ref、out参数

来源:互联网 发布:应用物理学 知乎 编辑:程序博客网 时间:2024/06/05 23:03

函数参数默认是值传递的,也就是“复制一份”,通过函数的处理对数值本身并没有影响,如果函数想对数值本身产生影响就需要使用相应的参数。

ref必须先初始化,因为是引用,所以必须先“有”,才能引用。使用ref如果未进行初始化,将报出如图所示的错误使用了未赋值的局部变量“age”:

加上ref之后传参传的是引用而不再是没加ref时的拷贝。

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace refout参数  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             int age;  
  13.             IncAge(ref age);   
  14.             Console.WriteLine(age);  
  15.             Console.ReadKey();  
  16.         }  
  17.         
  18.         static void IncAge(ref int age)  
  19.         {  
  20.             age++;  
  21.         }  
  22.         
  23.     }  
  24. }  

out是内部为外部赋值,所以不需要初始化,而且初始化也没用。

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace refout参数  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             int age;  
  13.             IncAge(out age);   
  14.             Console.WriteLine(age);  
  15.             Console.ReadKey();  
  16.         }  
  17.         
  18.         static void IncAge(out int age)  
  19.         {  
  20.             age=20;  
  21.         }  
  22.         
  23.     }  
  24. }  

执行结果:

总结:ref应用场景内部对外部的值进行改变,out则是内部为外部变量赋值,out一般用在函数有多个返回值的场所

out应用举例:int.TryParse

int.Parse()是一种类容转换;表示将数字内容的字符串转为int类型。
如果字符串为空,则抛出ArgumentNullException异常;
如果字符串内容不是数字,则抛出FormatException异常;
如果字符串内容所表示数字超出int类型可表示的范围,则抛出OverflowException异常;
int.TryParse 与 int.Parse 又较为类似,但它不会产生异常,转换成功返回 true,转换失败返回 false。最后一个参数为输出值,如果转换失败,输出值为 0

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace refout参数  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             string str = Console.ReadLine();  
  13.             int i;  
  14.             if (int.TryParse(str ,out i))  //i在内部被赋值  
  15.             {  
  16.                 Console.WriteLine("转换成功!{0}",i);  
  17.             }  
  18.             else  
  19.              {   
  20.                 Console.WriteLine("转换失败!{0}",i);  
  21.             }  
  22.             Console.ReadKey();  
  23.         }  
  24.     }  
  25. }  

执行结果:

转换成功:

转换失败:

ref应用举例:
交换函数Swap:

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace refout参数  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             int i1 = 10;  
  13.             int i2 = 20;  
  14.             Swap(ref i1,ref i2);  //注意ref参数  
  15.             Console.WriteLine("i1={0},i2={1}", i1, i2);  
  16.             Console.ReadKey();  
  17.         }  
  18.         static void Swap(ref int i1,ref int i2)  //注意ref参数  
  19.         {  
  20.             int temp = i1;  
  21.             i1 = i2;  
  22.             i2 = temp;  
  23.         }  
  24.     }  
  25. }  

如果不使用ref参数,Swap处理的将仅仅是i1和i2的拷贝,对值本身没有影响。所以数值没有改变。

使用ref参数则将引用传入函数,而不仅是值得拷贝。

执行结果:


 

原创粉丝点击