C#参数传递

参数传递

当调用带有参数的方法时,您需要向方法传递参数。在 C# 中,有三种向方法传递参数的方式:

方式 描述
值参数 这种方式复制参数的实际值给函数的形式参数,实参和形参使用的是两个不同内存中的值。在这种情况下,当形参的值发生改变时,不会影响实参的值,从而保证了实参数据的安全。
引用参数 这种方式复制参数的内存位置的引用给形式参数。这意味着,当形参的值发生改变时,同时也改变实参的值。
输出参数 这种方式可以返回多个值。

按值传递参数

这是参数传递的默认方式。在这种方式下,当调用一个方法时,会为每个值参数创建一个新的存储位置。

实际参数的值会复制给形参,实参和形参使用的是两个不同内存中的值。所以,当形参的值发生改变时,不会影响实参的值,从而保证了实参数据的安全。下面的实例演示了这个概念:

[csharp]  view plain  copy
  1. using System;  
  2. namespace CalculatorApplication  
  3. {  
  4.    class NumberManipulator  
  5.    {  
  6.       public void swap(int x, int y)  
  7.       {  
  8.          int temp;  
  9.            
  10.          temp = x; /* 保存 x 的值 */  
  11.          x = y;    /* 把 y 赋值给 x */  
  12.          y = temp; /* 把 temp 赋值给 y */  
  13.       }  
  14.         
  15.       static void Main(string[] args)  
  16.       {  
  17.          NumberManipulator n = new NumberManipulator();  
  18.          /* 局部变量定义 */  
  19.          int a = 100;  
  20.          int b = 200;  
  21.            
  22.          Console.WriteLine("在交换之前,a 的值: {0}", a);  
  23.          Console.WriteLine("在交换之前,b 的值: {0}", b);  
  24.            
  25.          /* 调用函数来交换值 */  
  26.          n.swap(a, b);  
  27.            
  28.          Console.WriteLine("在交换之后,a 的值: {0}", a);  
  29.          Console.WriteLine("在交换之后,b 的值: {0}", b);  
  30.            
  31.          Console.ReadLine();  
  32.       }  
  33.    }  
  34. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. 在交换之前,a 的值:100  
  2. 在交换之前,b 的值:200  
  3. 在交换之后,a 的值:100  
  4. 在交换之后,b 的值:200  

结果表明,即使在函数内改变了值,值也没有发生任何的变化。

按引用传递参数(ref)

引用参数是一个对变量的内存位置的引用。当按引用传递参数时,与值参数不同的是,它不会为这些参数创建一个新的存储位置。引用参数表示与提供给方法的实际参数具有相同的内存位置。

在 C# 中,使用 ref 关键字声明引用参数。下面的实例演示了这点:

[csharp]  view plain  copy
  1. using System;  
  2. namespace CalculatorApplication  
  3. {  
  4.    class NumberManipulator  
  5.    {  
  6.       public void swap(ref int x, ref int y)  
  7.       {  
  8.          int temp;  
  9.   
  10.          temp = x; /* 保存 x 的值 */  
  11.          x = y;    /* 把 y 赋值给 x */  
  12.          y = temp; /* 把 temp 赋值给 y */  
  13.        }  
  14.      
  15.       static void Main(string[] args)  
  16.       {  
  17.          NumberManipulator n = new NumberManipulator();  
  18.          /* 局部变量定义 */  
  19.          int a = 100;  
  20.          int b = 200;  
  21.   
  22.          Console.WriteLine("在交换之前,a 的值: {0}", a);  
  23.          Console.WriteLine("在交换之前,b 的值: {0}", b);  
  24.   
  25.          /* 调用函数来交换值 */  
  26.          n.swap(ref a, ref b);  
  27.   
  28.          Console.WriteLine("在交换之后,a 的值: {0}", a);  
  29.          Console.WriteLine("在交换之后,b 的值: {0}", b);  
  30.    
  31.          Console.ReadLine();  
  32.   
  33.       }  
  34.    }  
  35. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. 在交换之前,a 的值:100  
  2. 在交换之前,b 的值:200  
  3. 在交换之后,a 的值:200  
  4. 在交换之后,b 的值:100  

结果表明,swap 函数内的值改变了,且这个改变可以在 Main 函数中反映出来。

按输出传递参数(out)

return 语句可用于只从函数中返回一个值。但是,可以使用 输出参数 来从函数中返回两个值。输出参数会把方法输出的数据赋给自己,其他方面与引用参数相似。

下面的实例演示了这点:

[csharp]  view plain  copy
  1. using System;  
  2.   
  3. namespace CalculatorApplication  
  4. {  
  5.    class NumberManipulator  
  6.    {  
  7.       public void getValue(out int x )  
  8.       {  
  9.          int temp = 5;  
  10.          x = temp;  
  11.       }  
  12.      
  13.       static void Main(string[] args)  
  14.       {  
  15.          NumberManipulator n = new NumberManipulator();  
  16.          /* 局部变量定义 */  
  17.          int a = 100;  
  18.            
  19.          Console.WriteLine("在方法调用之前,a 的值: {0}", a);  
  20.            
  21.          /* 调用函数来获取值 */  
  22.          n.getValue(out a);  
  23.   
  24.          Console.WriteLine("在方法调用之后,a 的值: {0}", a);  
  25.          Console.ReadLine();  
  26.   
  27.       }  
  28.    }  
  29. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. 在方法调用之前,a 的值: 100  
  2. 在方法调用之后,a 的值: 5  

提供给输出参数的变量不需要赋值。当需要从一个参数没有指定初始值的方法中返回值时,输出参数特别有用。请看下面的实例,来理解这一点:

[csharp]  view plain  copy
  1. using System;  
  2.   
  3. namespace CalculatorApplication  
  4. {  
  5.    class NumberManipulator  
  6.    {  
  7.       public void getValues(out int x, out int y )  
  8.       {  
  9.           Console.WriteLine("请输入第一个值: ");  
  10.           x = Convert.ToInt32(Console.ReadLine());  
  11.           Console.WriteLine("请输入第二个值: ");  
  12.           y = Convert.ToInt32(Console.ReadLine());  
  13.       }  
  14.      
  15.       static void Main(string[] args)  
  16.       {  
  17.          NumberManipulator n = new NumberManipulator();  
  18.          /* 局部变量定义 */  
  19.          int a , b;  
  20.            
  21.          /* 调用函数来获取值 */  
  22.          n.getValues(out a, out b);  
  23.   
  24.          Console.WriteLine("在方法调用之后,a 的值: {0}", a);  
  25.          Console.WriteLine("在方法调用之后,b 的值: {0}", b);  
  26.          Console.ReadLine();  
  27.       }  
  28.    }  
  29. }  

当上面的代码被编译和执行时,它会产生下列结果(取决于用户输入):

[csharp]  view plain  copy
  1. 请输入第一个值:  
  2. 7  
  3. 请输入第二个值:  
  4. 8  
  5. 在方法调用之后,a 的值: 7  
  6. 在方法调用之后,b 的值: 8  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值