当前位置:编程学习 > C#/ASP.NET >>

C#中的引用传递、值传递

 一、传递参数
既可以通过值也可以通过引用传递参数。通过引用传递参数允许函数成员(方法、属性、索引器、运算符和构造函数)更改参数的值,并保持该更改。
 
二、传递值类型参数
 值类型变量直接包含其数据,这与引用类型变量不同,后者包含对其数据的引用。因此,向方法传递值类型变量意味着向方法传递变量的一个副本。方法内发生的对参数的更改对该变量中存储的原始数据无任何影响。如果希望所调用的方法更改参数的值,必须使用 ref 或 out 关键字通过引用传递该参数。为了简单起见,下面的示例使用 ref。
 
1. 通过值传递值类型:
[csharp] 
class PassingValByVal  
{  
    static void SquareIt(int x)  
    // The parameter x is passed by value.  
    // Changes to x will not affect the original value of x.  
    {  
        x *= x;  
        System.Console.WriteLine("The value inside the method: {0}", x);  
    }  
    static void Main()  
    {  
        int n = 5;  
        System.Console.WriteLine("The value before calling the method: {0}", n);  
SquareIt(n);  // Passing the variable by value.  www.zzzyk.com
    System.Console.WriteLine("The value after calling the method: {0}", n);  
    }  
}  
运行结果为:
变量 n 为值类型,包含其数据(值为 5)。当调用 SquareIt 时,n 的内容被复制到参数 x 中,在方法内将该参数求平方。但在 Main 中,n 的值在调用 SquareIt 方法前后是相同的。实际上,方法内发生的更改只影响局部变量 x。
2.通过引用传递值类型
下面的示例除使用 ref 关键字传递参数以外,其余与上一示例相同。参数的值在调用方法后发生更改
 
[csharp]  
class PassingValByRef  
{  
    static void SquareIt(ref int x)  
    // The parameter x is passed by reference.  
    // Changes to x will affect the original value of x.  
    {  
        x *= x;  
        System.Console.WriteLine("The value inside the method: {0}", x);  
    }  
    static void Main()  
    {  
        int n = 5;  
        System.Console.WriteLine("The value before calling the method: {0}", n);  
SquareIt(ref n);  // Passing the variable by reference.  
        System.Console.WriteLine("The value after calling the method: {0}", n);  
    }  
}  
 
运行结果为:
本示例中,传递的不是 n 的值,而是对 n 的引用。参数 x 不是 int 类型,它是对 int 的引用(本例中为对 n 的引用)。因此,当在方法内对 x 求平方时,实际被求平方的是 x 所引用的项:n。
 
 
 
3. 交换值类型
更改所传递参数的值的常见示例是 Swap 方法,在该方法中传递 x 和 y 两个变量,然后使方法交换它们的内容。必须通过引用向 Swap 方法传递参数;否则,方法内所处理的将是参数的本地副本。以下是使用引用参数的 Swap 方法的示例:
[csharp]  
static void SwapByRef(ref int x, ref int y)  
{  
    int temp = x;  
    x = y;  
    y = temp;  
}  
 
三、传递引用类型参数
引用类型的变量不直接包含其数据;它包含的是对其数据的引用。当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值。但是无法更改引用本身的值;也就是说,不能使用相同的引用为新类分配内存并使之在块外保持。若要这样做,应使用 ref 或 out 关键字传递参数。为了简单起见,下面的示例使用 ref。
1. 通过值传递引用类型
下面的示例演示通过值向 Change 方法传递引用类型的参数 arr。由于该参数是对 arr 的引用,所以有可能更改数组元素的值。但是,试图将参数重新分配到不同的内存位置时,该操作仅在方法内有效,并不影响原始变量 arr。
[csharp]  
class PassingRefByVal   
{  
    static void Change(int[] pArray)  
    {  
        pArray[0] = 888;  // This change affects the original element.  
        pArray = new int[5] {-3, -1, -2, -3, -4};   // This change is local.  
        System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);  
    }  
  
    static void Main()   
    {  
        int[] arr = {1, 4, 5};  
        System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr [0]);  
  
        Change(arr);  
        System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr [0]);  
    }  
}  
运行结果为:
 
在上个示例中,数组 arr 为引用类型,在未使用 ref 参数的情况下传递给方法。在此情况下,将向方法传递指向 arr 的引用的一个副本。输出显示方法有可能更改数组元素的内容,在这种情况下,从 1改为 888。但是,在 Change 方法内使用 new 运算符来分配新的内存部分,将使变量 pArray 引用新的数组。因此,这之后的任何更改都不会影响原始数组 arr(它是在 Main 内创建的)。实际上,本示例中创建了两个数组,一个在 Main 内,一个在 Change 方法内。
2. 通过引用传递引用类型
本示例除在方法头和调用中使用 ref 关键字以外,其余与上个示例相同。方法内发生的任何更改都会影响调用程序中的原始变量
[csharp]  
class PassingRefByRef   
{  
    static void Change(ref int[] pArray)  
    {  
        // Both of the following changes will affect the original variables:  
        pArray[0] = 888;  
        pArray = new int[5] {-3, -1, -2, -3, -4};  
        System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);  
    }  
  
    static void Main()   
    {  
        int[] arr = {1, 4, 5};  
        System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]);  
  
        Change(ref arr);  
        System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]);  
    }  
}  
 
运行结果为:
方法内
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,