引用传递、值传递

    先来讲下C#中的数据类型.分值类型和引用类型两大类.数组

  值类型:直接存储数据的值,保存在内存中ide

  引用类型:存储对值的引用,实际上存储的就是一个内存的地址函数

  C#预约义的简单类型,像int,float,bool,char都是值类型,另外enum(枚举),struct(结构)也是值类型spa

  string,数组,自定义的class类、接口、委托和封装就都是引用类型了.其中的string是比较特殊的引用类型code

  C#函数的参数若是不加ref,out这样的修饰符显式申明参数是经过引用传递外,默认都是值传递.blog

 

传递值类型参数

   值类型变量直接包含其数据,这与引用类型变量不一样,后者包含对其数据的引用。所以,向方法传递值类型变量意味着向方法传递变量的一个副本。方法内发生的对参数的更改对该变量中存储的原始数据无任何影响。若是但愿所调用的方法更改参数的值,必须使用 ref 或 out 关键字经过引用传递该参数。为了简单起见,下面的示例使用 ref接口

  1. 经过值传递值类型:

代码

class PassingValByVal
{
    static void SquareIt(int x)
    {
        x *= x;
        System.Console.WriteLine("The value inside the method: {0}", x);  //25
    }
    static void Main()
    {
        int n = 5;
        System.Console.WriteLine("The value before calling the method: {0}", n);  //5

        SquareIt(n); 
        System.Console.WriteLine("The value after calling the method: {0}", n);  //5
    }
}
变量  n 为值类型,包含其数据(值为  5)。当调用  SquareIt 时, n 的内容被复制到参数  x 中,在方法内将该参数求平方。但在  Main 中, n 的值在调用  SquareIt 方法先后是相同的。实际上,方法内发生的更改只影响局部变量  x

2.经过引用传递值类型

下面的示例除使用 ref 关键字传递参数之外,其他与上一示例相同。参数的值在调用方法后发生更改内存

代码

class PassingValByRef
{
    static void SquareIt(ref int x)
    {
        x *= x;
        System.Console.WriteLine("The value inside the method: {0}", x);//25
    }
    static void Main()
    {
        int n = 5;
        System.Console.WriteLine("The value before calling the method: {0}", n);//5

        SquareIt(ref n); 
        System.Console.WriteLine("The value after calling the method: {0}", n);//25
    }
}

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

传递引用类型参数

  引用类型的变量不直接包含其数据;它包含的是对其数据的引用。 当经过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值。可是没法更改引用自己的值;也就是说,不能使用相同的引用为新类分配内存并使之在块外保持。若要这样作,应使用 ref 或 out 关键字传递参数。为了简单起见,下面的示例使用  ref

  1. 经过值传递引用类型

下面的示例演示经过值向  Change 方法传递引用类型的参数  arr。因为该参数是对  arr 的引用,因此有可能更改数组元素的值。可是,试图将参数从新分配到不一样的内存位置时,该操做仅在方法内有效,并不影响原始变量  arr
代码

class PassingRefByVal 
{
    static void Change(int[] pArray)
    {
        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]);//-3
    }

    static void Main() 
    {
        int[] arr = {1, 4, 5};
        System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr [0]);//1

        Change(arr);
        System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr [0]);//888
    }
}
在上个示例中,数组  arr 为引用类型,在未使用  ref 参数的状况下传递给方法。在此状况下,将向方法传递指向 arr 的引用的一个副本。输出显示方法有可能更改数组元素的内容,在这种状况下,从 1改成 888。可是,在 Change 方法内使用 new 运算符来分配新的内存部分,将使变量 pArray 引用新的数组。所以,这以后的任何更改都不会影响原始数组 arr(它是在 Main 内建立的)。实际上,本示例中建立了两个数组,一个在 Main 内,一个在 Change 方法内。

  2. 经过引用传递引用类型

本示例除在方法头和调用中使用  ref 关键字之外,其他与上个示例相同。方法内发生的任何更改都会影响调用程序中的原始变量
class PassingRefByRef 
{
    static void Change(ref int[] pArray)
    {
        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]);//-3
    }
        
    static void Main() 
    {
        int[] arr = {1, 4, 5};
        System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]);//1

        Change(ref arr);
        System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]);//-3
    }
}
方法内发生的全部更改都影响  Main 中的原始数组。实际上,使用  new 运算符对原始数组进行了从新分配。所以,调用  Change 方法后,对  arr 的任何引用都将指向  Change 方法中建立的五个元素的数组。