国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#中的值傳遞和引用傳遞詳細(xì)解析

2020-01-24 02:54:35
字體:
供稿:網(wǎng)友

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

二、傳遞值類型參數(shù)
值類型變量直接包含其數(shù)據(jù),這與引用類型變量不同,后者包含對其數(shù)據(jù)的引用。因此,向方法傳遞值類型變量意味著向方法傳遞變量的一個(gè)副本。方法內(nèi)發(fā)生的對參數(shù)的更改對該變量中存儲(chǔ)的原始數(shù)據(jù)無任何影響。如果希望所調(diào)用的方法更改參數(shù)的值,必須使用 ref 或 out 關(guān)鍵字通過引用傳遞該參數(shù)。為了簡單起見,下面的示例使用 ref。

1. 通過值傳遞值類型:

復(fù)制代碼 代碼如下:

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 為值類型,包含其數(shù)據(jù)(值為 5)。當(dāng)調(diào)用 SquareIt 時(shí),n 的內(nèi)容被復(fù)制到參數(shù) x 中,在方法內(nèi)將該參數(shù)求平方。但在 Main 中,n 的值在調(diào)用 SquareIt 方法前后是相同的。實(shí)際上,方法內(nèi)發(fā)生的更改只影響局部變量 x。

2.通過引用傳遞值類型
下面的示例除使用 ref 關(guān)鍵字傳遞參數(shù)以外,其余與上一示例相同。參數(shù)的值在調(diào)用方法后發(fā)生更改

復(fù)制代碼 代碼如下:

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 的引用。參數(shù) x 不是 int 類型,它是對 int 的引用(本例中為對 n 的引用)。因此,當(dāng)在方法內(nèi)對 x 求平方時(shí),實(shí)際被求平方的是 x 所引用的項(xiàng):n。

3. 交換值類型
更改所傳遞參數(shù)的值的常見示例是 Swap 方法,在該方法中傳遞 x 和 y 兩個(gè)變量,然后使方法交換它們的內(nèi)容。必須通過引用向 Swap 方法傳遞參數(shù);否則,方法內(nèi)所處理的將是參數(shù)的本地副本。以下是使用引用參數(shù)的 Swap 方法的示例:

復(fù)制代碼 代碼如下:

static void SwapByRef(ref int x, ref int y)
{
    int temp = x;
    x = y;
    y = temp;
}

三、傳遞引用類型參數(shù)
引用類型的變量不直接包含其數(shù)據(jù);它包含的是對其數(shù)據(jù)的引用。當(dāng)通過值傳遞引用類型的參數(shù)時(shí),有可能更改引用所指向的數(shù)據(jù),如某類成員的值。但是無法更改引用本身的值;也就是說,不能使用相同的引用為新類分配內(nèi)存并使之在塊外保持。若要這樣做,應(yīng)使用 ref 或 out 關(guān)鍵字傳遞參數(shù)。為了簡單起見,下面的示例使用 ref。

1. 通過值傳遞引用類型
下面的示例演示通過值向 Change 方法傳遞引用類型的參數(shù) arr。由于該參數(shù)是對 arr 的引用,所以有可能更改數(shù)組元素的值。但是,試圖將參數(shù)重新分配到不同的內(nèi)存位置時(shí),該操作僅在方法內(nèi)有效,并不影響原始變量 arr。

復(fù)制代碼 代碼如下:

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]);
}
}


在上個(gè)示例中,數(shù)組 arr 為引用類型,在未使用 ref 參數(shù)的情況下傳遞給方法。在此情況下,將向方法傳遞指向 arr 的引用的一個(gè)副本。輸出顯示方法有可能更改數(shù)組元素的內(nèi)容,在這種情況下,從 1改為 888。但是,在 Change 方法內(nèi)使用 new 運(yùn)算符來分配新的內(nèi)存部分,將使變量 pArray 引用新的數(shù)組。因此,這之后的任何更改都不會(huì)影響原始數(shù)組 arr(它是在 Main 內(nèi)創(chuàng)建的)。實(shí)際上,本示例中創(chuàng)建了兩個(gè)數(shù)組,一個(gè)在 Main 內(nèi),一個(gè)在 Change 方法內(nèi)。

2. 通過引用傳遞引用類型
本示例除在方法頭和調(diào)用中使用 ref 關(guān)鍵字以外,其余與上個(gè)示例相同。方法內(nèi)發(fā)生的任何更改都會(huì)影響調(diào)用程序中的原始變量

復(fù)制代碼 代碼如下:

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]);
    }
}


方法內(nèi)發(fā)生的所有更改都影響 Main 中的原始數(shù)組。實(shí)際上,使用 new 運(yùn)算符對原始數(shù)組進(jìn)行了重新分配。因此,調(diào)用 Change 方法后,對 arr 的任何引用都將指向 Change 方法中創(chuàng)建的五個(gè)元素的數(shù)組。

3. 交換兩個(gè)字符串
交換字符串是通過引用傳遞引用類型參數(shù)的很好的示例。本示例中,str1 和 str2 兩個(gè)字符串在 Main 中初始化,并作為由 ref 關(guān)鍵字修改的參數(shù)傳遞給 SwapStrings 方法。這兩個(gè)字符串在該方法內(nèi)以及Main 內(nèi)均進(jìn)行交換。

復(fù)制代碼 代碼如下:

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);
}
}


本示例中,需要通過引用傳遞參數(shù)以影響調(diào)用程序中的變量。如果同時(shí)從方法頭和方法調(diào)用中移除 ref 關(guān)鍵字,則調(diào)用程序中不會(huì)發(fā)生任何更改。

四、引用類型的數(shù)據(jù)值傳遞(復(fù)本傳遞)
類的默認(rèn)用MemberwiseClone 方法創(chuàng)建一個(gè)淺表副本,方法是創(chuàng)建一個(gè)新對象,然后將當(dāng)前對象的非靜態(tài)字段復(fù)制到該新對象。如果字段是值類型的,則對該字段執(zhí)行逐位復(fù)制。如果字段是引用 類型,則復(fù)制引用但不復(fù)制引用的對象;因此,原始對象及其復(fù)本引用同一對象。深拷貝,即實(shí)現(xiàn)ICloneable接口.ICloneable可用于深拷貝 和淺拷貝。這些都是概念,但是需要我們理解:

復(fù)制代碼 代碼如下:

   class ClassA : ICloneable
    {
        public string str;
        public SubClass subclass;
        public ClassA()
        {
            str = "classA str";
            subclass = new SubClass();
        }
        //深復(fù)制,多層不可用MemberwiseClone()完整實(shí)現(xiàn)深復(fù)制
        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";
        }
        //深復(fù)制,因?yàn)橹灰粚樱钥梢杂肕emberwiseClone()方法
        public object Clone()
        {
            this.str = (string)this.str.Clone();
            return this.MemberwiseClone();
        }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 班戈县| 布拖县| 商河县| 池州市| 湘西| 炎陵县| 平乡县| 姚安县| 广丰县| 乌海市| 彩票| 新营市| 石景山区| 金乡县| 莲花县| 通州市| 赤峰市| 紫阳县| 资阳市| 福安市| 竹北市| 筠连县| 吉隆县| 宜城市| 隆子县| 阿拉善盟| 新密市| 东乡族自治县| 望都县| 南皮县| 西充县| 垣曲县| 英吉沙县| 米林县| 讷河市| 丹棱县| 沭阳县| 澄城县| 红桥区| 桦南县| 宜宾县|