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


 

一、傳遞參數

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

二、傳遞值類型參數

   值類型變量直接包含其數據,這與引用類型變量不同,后者包含對其數據的引用。因此,向方法傳遞值類型變量意味着向方法傳遞變量的一個副本。方法內發生的對參數的更改對該變量中存儲的原始數據無任何影響。如果希望所調用的方法更改參數的值,必須使用 ref 或 out 關鍵字通過引用傳遞該參數。為了簡單起見,下面的示例使用 ref

  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 關鍵字傳遞參數以外,其余與上一示例相同。參數的值在調用方法后發生更改

代碼

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;
}
復制代碼

 

三、傳遞引用類型參數

  引用類型的變量不直接包含其數據;它包含的是對其數據的引用。當通過值傳遞引用類型的參數時,有可能更改引用所指向的數據,如某類成員的值。但是無法更改引用本身的值;也就是說,不能使用相同的引用為新類分配內存並使之在塊外保持。若要這樣做,應使用 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 關鍵字,則調用程序中不會發生任何更改。
 

四、引用類型的數據值傳遞(復本傳遞)

  類的默認用MemberwiseClone 方法創建一個淺表副本,方法是創建一個新對象,然后將當前對象的非靜態字段復制到該新對象。如果字段是值類型的,則對該字段執行逐位復制。如果字段是引用 類型,則復制引用但不復制引用的對象;因此,原始對象及其復本引用同一對象。深拷貝,即實現ICloneable接口.ICloneable可用於深拷貝 和淺拷貝。這些都是概念,但是需要我們理解:

 

代碼

    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();
}
復制代碼

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM