C#中的引用傳遞、值傳遞。

 

1、傳遞參數

  既能夠經過值也能夠經過引用傳遞參數。經過引用傳遞參數容許函數成員(方法、屬性、索引器、運算符和構造函數)更改參數的值,並保持該更改。數組

2、傳遞值類型參數

   值類型變量直接包含其數據,這與引用類型變量不一樣,後者包含對其數據的引用。所以,向方法傳遞值類型變量意味着向方法傳遞變量的一個副本。方法內發生的對參數的更改對該變量中存儲的原始數據無任何影響。若是但願所調用的方法更改參數的值,必須使用 ref 或 out 關鍵字經過引用傳遞該參數。爲了簡單起見,下面的示例使用 refapp

  1. 經過值傳遞值類型:

代碼

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.
        System.Console.WriteLine("The value after calling the method: {0}", n);
    }
}

  

變量  n 爲值類型,包含其數據(值爲  5)。當調用  SquareIt 時, n 的內容被複制到參數  x 中,在方法內將該參數求平方。但在  Main 中, n 的值在調用  SquareIt 方法先後是相同的。實際上,方法內發生的更改隻影響局部變量  x

2.經過引用傳遞值類型

下面的示例除使用 ref 關鍵字傳遞參數之外,其他與上一示例相同。參數的值在調用方法後發生更改ide

代碼

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 方法的示例:
複製代碼
static void SwapByRef(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}
複製代碼

 

3、傳遞引用類型參數

  引用類型的變量不直接包含其數據;它包含的是對其數據的引用。當經過值傳遞引用類型的參數時,有可能更改引用所指向的數據,如某類成員的值。可是沒法更改引用自己的值;也就是說,不能使用相同的引用爲新類分配內存並使之在塊外保持。若要這樣作,應使用 ref 或 out 關鍵字傳遞參數。爲了簡單起見,下面的示例使用  ref

  1. 經過值傳遞引用類型

下面的示例演示經過值向  Change 方法傳遞引用類型的參數  arr。因爲該參數是對  arr 的引用,因此有可能更改數組元素的值。可是,試圖將參數從新分配到不一樣的內存位置時,該操做僅在方法內有效,並不影響原始變量  arr
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 關鍵字之外,其他與上個示例相同。方法內發生的任何更改都會影響調用程序中的原始變量
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]);
    }
}

  

方法內發生的全部更改都影響  Main 中的原始數組。實際上,使用  new 運算符對原始數組進行了從新分配。所以,調用  Change 方法後,對  arr 的任何引用都將指向  Change 方法中建立的五個元素的數組。

  3. 交換兩個字符串

交換字符串是經過引用傳遞引用類型參數的很好的示例。本示例中, str1 和  str2 兩個字符串在  Main 中初始化,並做爲由  ref 關鍵字修改的參數傳遞給  SwapStrings 方法。這兩個字符串在該方法內以及 Main 內均進行交換。
class SwappingStrings
{
    static void SwapStrings(ref string s1, ref string s2)
    // The string parameter is passed by reference.
    // Any changes on parameters will affect the original variables.
    {
        string temp = s1;
        s1 = s2;
        s2 = temp;
        System.Console.WriteLine("Inside the method: {0} {1}", s1, s2);
    }

    static void Main()
    {
        string str1 = "John";
        string str2 = "Smith";
        System.Console.WriteLine("Inside Main, before swapping: {0} {1}", str1, str2);

        SwapStrings(ref str1, ref str2);   // Passing strings by reference
        System.Console.WriteLine("Inside Main, after swapping: {0} {1}", str1, str2);
    }
}

  


複製代碼
class SwappingStrings
{
static void SwapStrings(ref string s1, ref string s2)
// The string parameter is passed by reference.
// Any changes on parameters will affect the original variables.
{
string temp = s1;
s1 = s2;
s2 = temp;
System.Console.WriteLine("Inside the method: {0} {1}", s1, s2);
}

static void Main()
{
string str1 = "John";
string str2 = "Smith";
System.Console.WriteLine("Inside Main, before swapping: {0} {1}", str1, str2);

SwapStrings(ref str1, ref str2); // Passing strings by reference
System.Console.WriteLine("Inside Main, after swapping: {0} {1}", str1, str2);
}
}
複製代碼

 

本示例中,須要經過引用傳遞參數以影響調用程序中的變量。若是同時從方法頭和方法調用中移除  ref 關鍵字,則調用程序中不會發生任何更改。
 

4、引用類型的數據值傳遞(複本傳遞)

  類的默認用MemberwiseClone 方法建立一個淺表副本,方法是建立一個新對象,而後將當前對象的非靜態字段複製到該新對象。若是字段是值類型的,則對該字段執行逐位複製。若是字段是引用 類型,則複製引用但不復制引用的對象;所以,原始對象及其複本引用同一對象。深拷貝,即實現ICloneable接口.ICloneable可用於深拷貝 和淺拷貝。這些都是概念,可是須要咱們理解:post

 

代碼

    class ClassA : ICloneable
    {
        public string str;
        public SubClass subclass;
        public ClassA()
        {
            str = "classA str";
            subclass = new SubClass();
        }
        //深複製,多層不可用MemberwiseClone()完整實現深複製
        public object Clone()
        {
           // this.a = (string)this.a.Clone();
            //this.b = (B)this.b.Clone();
            var ne = new ClassA();
            ne.str = this.str;
            ne.subclass = (SubClass)this.subclass.Clone(); //this.b的話仍是沒有成功
            return ne;
           // return this.MemberwiseClone();
        }
    }

    class SubClass : ICloneable
    {
        public string str;
        public SubClass()
        {
            this.str = "subclass str";
        }
        //深複製,由於只一層,因此能夠用MemberwiseClone()方法
        public object Clone()
        {
            this.str = (string)this.str.Clone();
            return this.MemberwiseClone();
        }

  


複製代碼
    class ClassA : ICloneable
{
public string str;
public SubClass subclass;
public ClassA()
{
str = "classA str";
subclass = new SubClass();
}
//深複製,多層不可用MemberwiseClone()完整實現深複製
public object Clone()
{
// this.a = (string)this.a.Clone();
//this.b = (B)this.b.Clone();
var ne = new ClassA();
ne.str = this.str;
ne.subclass = (SubClass)this.subclass.Clone(); //this.b的話仍是沒有成功
return ne;
// return this.MemberwiseClone();
}
}

class SubClass : ICloneable
{
public string str;
public SubClass()
{
this.str = "subclass str";
}
//深複製,由於只一層,因此能夠用MemberwiseClone()方法
public object Clone()
{
this.str = (string)this.str.Clone();
return this.MemberwiseClone();
}
複製代碼
相關文章
相關標籤/搜索