按值按引用传递参数

CLR默认所有的方法都是按值传递参数的。

传递值类型参数
值类型变量直接存储其数据,也就是说,向方法传递一个值类型参数,传递的是这个参数在堆栈上的一个副本,方法内对这个参数副本做的更改不能影响到原来参数中的数据。下面是MSDN中的一个例子。

注意:示例 1 到 示例 3 都是传递的值类型,示例 4 到 示例 6 传递的是引用类型

示例 1:通过值传递值类型
下面的示例演示通过值传递值类型参数。通过值将变量 myInt 传递给方法 SquareIt。方法内发生的任何更改对变量的原始值无任何影响。

None.gif //  PassingParams1.cs 
None.gif
using  System;
None.gif
class  PassingValByVal
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static void SquareIt(int x)
InBlock.gif    
// The parameter x is passed by value.
InBlock.gif    
// Changes to x will not affect the original value of myInt.
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        x 
*= x;
InBlock.gif        Console.WriteLine(
"The value inside the method: {0}", x);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int myInt = 5;
InBlock.gif        Console.WriteLine(
"The value before calling the method: {0}",
InBlock.gif           myInt);
InBlock.gif        SquareIt(myInt);   
// Passing myInt by value.
InBlock.gif
        Console.WriteLine("The value after calling the method: {0}",
InBlock.gif           myInt);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

输出

The value before calling the method: 5
The value inside the method: 25
The value after calling the method: 5

变量 myInt 为值类型,包含其数据(值 5)。当调用 SquareIt 时,myInt 的内容被复制到参数 x 中,在方法内将该参数求平方。但在 Main 中,myInt 的值在调用 SquareIt 方法之前和之后是相同的。实际上,方法内发生的更改只影响局部变量 x。

如果我们希望所调用的方法改变参数的值,那么我们要用传递引用的方式来传递一个参数。C#提供了两个关键字:ref和out(他们的差别会在最后讨论)。这两个关键字告诉C#编译器,参数是按引用传递的,此时,传递的是参数在的地址,而不是参数本身的值。

再看MSDN中的例子:

示例 2:通过引用传递值类型
下面的示例除使用 ref 关键字传递参数以外,其余与“示例 1”相同。参数的值在调用方法后发生更改。

None.gif //  PassingParams2.cs 
None.gif
using  System;
None.gif
class  PassingValByRef
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static void SquareIt(ref int x)
InBlock.gif    
// The parameter x is passed by reference.
InBlock.gif    
// Changes to x will affect the original value of myInt.
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        x 
*= x;
InBlock.gif        Console.WriteLine(
"The value inside the method: {0}", x);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int myInt = 5;
InBlock.gif        Console.WriteLine(
"The value before calling the method: {0}",
InBlock.gif           myInt);
InBlock.gif        SquareIt(
ref myInt);   // Passing myInt by reference.
InBlock.gif
        Console.WriteLine("The value after calling the method: {0}",
InBlock.gif           myInt);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

输出

The value before calling the method: 5
The value inside the method: 25
The value after calling the method: 25

本示例中,传递的不是 myInt 的值,而是对 myInt 的引用。参数 x 不是 int 类型,它是对 int 的引用(本例中为对 myInt 的引用)。因此,当在方法内对 x 求平方时,实际被求平方的是 x 所引用的项:myInt。

示例 3:交换值类型
更改所传递参数的值的常见示例是 Swap 方法,在该方法中传递 x 和 y 两个变量,然后使方法交换它们的内容。必须通过引用向 Swap 方法传递参数;否则,方法内所处理的将是参数的本地副本。以下是使用引用参数的 Swap 方法的示例:

None.gif static   void  SwapByRef( ref   int  x,  ref   int  y)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int temp = x;
InBlock.gif    x 
= y;
InBlock.gif    y 
= temp;
ExpandedBlockEnd.gif}

调用该方法时,请在调用中使用 ref 关键字,如下所示:

SwapByRef (ref i, ref j);

传递引用类型参数
引用类型的变量不直接包含其数据,它包含的是对其数据的引用,也就是数据在托管推上的地址。当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值。但是无法更改引用本身的值;也就是说,不能使用相同的引用为新类分配内存并使之在块外保持。若要这样做,请使用 ref(或 out)关键字传递参数。为了简单起见,以下示例使用 ref。

示例 4:通过值传递引用类型
下面的示例演示通过值向 Change 方法传递引用类型的参数 myArray。由于该参数是对 myArray 的引用,所以有可能更改数组元素的值。但是,试图将参数重新分配到不同的内存位置时,该操作仅在方法内有效,并不影响原始变量 myArray。

None.gif //  PassingParams4.cs 
None.gif
//  Passing an array to a method without the ref keyword.
None.gif
//  Compare the results to those of Example 5.
None.gif
using  System;
None.gif
class  PassingRefByVal 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
static void Change(int[] arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      arr[
0]=888;   // This change affects the original element.
ExpandedSubBlockStart.gifContractedSubBlock.gif
      arr = new int[5dot.gif{-3-1-2-3-4};   // This change is local.
InBlock.gif
      Console.WriteLine("Inside the method, the first element is: {0}", arr[0]);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
InBlock.gif   
public static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
int[] myArray = dot.gif{1,4,5};
InBlock.gif      Console.WriteLine(
"Inside Main, before calling the method, the first element is: {0}", myArray [0]);
InBlock.gif      Change(myArray);
InBlock.gif      Console.WriteLine(
"Inside Main, after calling the method, the first element is: {0}", myArray [0]);
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

输出

Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: 888

代码讨论
在上个示例中,数组 myArray 为引用类型,在未使用 ref 参数的情况下传递给方法。在此情况下,将向方法传递指向 myArray 的引用的一个副本。输出显示方法有可能更改数组元素的内容(从 1 改为 888)。但是,在 Change 方法内使用 new 运算符分配新的内存部分,将使变量 arr 引用新的数组。因此,这之后的任何更改都不会影响原始数组 myArray(它是在 Main 内创建的)。实际上,本示例中创建了两个数组,一个在 Main 内,一个在 Change 方法内。

示例 5:通过引用传递引用类型
本示例除在方法头和调用中使用 ref 关键字以外,其余与“示例 4”相同。方法内发生的任何更改都会影响调用程序中的原始变量。

None.gif //  PassingParams5.cs 
None.gif
//  Passing an array to a method with the ref keyword.
None.gif
//  Compare the results to those of Example 4.
None.gif
using  System;
None.gif
class  PassingRefByRef 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
static void Change(ref int[] arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
// Both of the following changes will affect the original variables:
InBlock.gif
      arr[0]=888;
ExpandedSubBlockStart.gifContractedSubBlock.gif      arr 
= new int[5dot.gif{-3-1-2-3-4};
InBlock.gif
InBlock.gif      Console.WriteLine(
"Inside the method, the first element is: {0}", arr[0]);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
InBlock.gif   
public static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
int[] myArray = dot.gif{1,4,5};
InBlock.gif      Console.WriteLine(
"Inside Main, before calling the method, the first element is: {0}", myArray [0]);
InBlock.gif      Change(
ref myArray);
InBlock.gif      Console.WriteLine(
"Inside Main, after calling the method, the first element is: {0}", myArray [0]);
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

输出

Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: -3

代码讨论
方法内发生的所有更改都影响 Main 中的原始数组。实际上,使用 new 运算符对原始数组进行了重新分配。因此,调用 Change 方法后,对 myArray 的任何引用都将指向 Change 方法中创建的五个元素的数组。

示例 6:交换两个字符串
交换字符串是通过引用传递引用类型参数的很好的示例。本示例中,str1 和 str2 两个字符串在 Main 中初始化,并作为由 ref 关键字修饰的参数传递给 SwapStrings 方法。这两个字符串在该方法内以及 Main 内均进行交换。

None.gif //  PassingParams6.cs
None.gif
using  System;
None.gif
class  SwappinStrings
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static void SwapStrings(ref string s1, ref string s2)
InBlock.gif    
// The string parameter x is passed by reference.
InBlock.gif    
// Any changes on parameters will affect the original variables.
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
string temp = s1;
InBlock.gif        s1 
= s2;
InBlock.gif        s2 
= temp;
InBlock.gif        Console.WriteLine(
"Inside the method: {0}, {1}", s1, s2);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string str1 = "John";
InBlock.gif        
string str2 = "Smith";
InBlock.gif        Console.WriteLine(
"Inside Main, before swapping: {0} {1}"
InBlock.gif           str1, str2);
InBlock.gif        SwapStrings(
ref str1, ref str2);   // Passing strings by reference
InBlock.gif
        Console.WriteLine("Inside Main, after swapping: {0}, {1}"
InBlock.gif           str1, str2);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

输出

Inside Main, before swapping: John Smith
Inside the method: Smith, John
Inside Main, after swapping: Smith, John

代码讨论
本示例中,需要通过引用传递参数以影响调用程序中的变量。如果同时从方法头和方法调用中移除 ref 关键字,则调用程序中不会发生任何更改。

CLR允许我们根据ref和out关键字来重载方法,比如:

None.gif class  Chengbo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void Add(int a) dot.gif{}
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void Add(ref int a) dot.gif{}
ExpandedBlockEnd.gif}

但是仅通过区分ref和out来重载方法是不合法的,因为他们经过JIT(just in time)编译后的代码是一样的。

传递到 ref 参数的参数必须最先初始化。将此方法与 out 参数相比,后者的参数在传递到 out 参数之前不必显式初始化。如下:

None.gif //  cs_out.cs 
None.gif
using  System;
None.gif
public   class  MyClass 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
public static int TestOut(out char i) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      i 
= 'b';
InBlock.gif      
return -1;
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
char i;   // variable need not be initialized
InBlock.gif
      Console.WriteLine(TestOut(out i));
InBlock.gif      Console.WriteLine(i);
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

输出

-1
b

示例

None.gif //  cs_ref.cs
None.gif
using  System;
None.gif
public   class  MyClass 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
public static void TestRef(ref char i) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
// The value of i will be changed in the calling method
InBlock.gif
      i = 'b';
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public static void TestNoRef(char i) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
// The value of i will be unchanged in the calling method
InBlock.gif
      i = 'c';
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
// This method passes a variable as a ref parameter; the value of the 
InBlock.gif   
// variable is changed after control passes back to this method.
InBlock.gif   
// The same variable is passed as a value parameter; the value of the
InBlock.gif   
// variable is unchanged after control is passed back to this method.
InBlock.gif
   public static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif   
InBlock.gif      
char i = 'a';    // variable must be initialized
InBlock.gif
      TestRef(ref i);  // the arg must be passed as ref
InBlock.gif
      Console.WriteLine(i);
InBlock.gif      TestNoRef(i);
InBlock.gif      Console.WriteLine(i);
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

输出

b
b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值