Share via


ref (C# 參考)

ref 關鍵字會導致引數以傳址方式傳遞的,沒有值。 透過參考的作用是對參數的任何變更都會在方法反映基礎引數變數在呼叫的方法上。 參考參數中的值總是與基礎引數變數的值。

注意事項注意事項

請混淆傳址 (By Reference) 的概念以及參考型別 (Reference Type) 的概念。這兩個概念並不相同。無論方法參數是實值型別或參考型別,皆可由 ref 修改。以傳址 (By Reference) 方式傳遞實值型別時,不會發生實值型別的 Boxing。

如下列範例所示,要使用 ref 參數,方法定義和呼叫方法都必須明確使用關鍵字, ref 。

    class RefExample
    {
        static void Method(ref int i)
        {
            // Rest the mouse pointer over i to verify that it is an int.
            // The following statement would cause a compiler error if i
            // were boxed as an object.
            i = i + 44;
        }

        static void Main()
        {
            int val = 1;
            Method(ref val);
            Console.WriteLine(val);

            // Output: 45
        }
    }

傳遞至 ref 參數的引數必須初始化,在傳遞之前。 這與 out 參數不同,引數並不需要明確初始化,並在傳遞之前。 如需詳細資訊,請參閱 out

類別的成員不能有由 ref 和 out只不同的簽章。 編譯器錯誤,如果型別之間的成員的唯一差異是其中一個有一 ref 參數,而另一 out 參數。 下列程式碼,例如,無法編譯。

class CS0663_Example
{
    // Compiler error CS0663: "Cannot define overloaded 
    // methods that differ only on ref and out".
    public void SampleMethod(out int i) { }
    public void SampleMethod(ref int i) { }
}

然而,多載可以做,當一個方法有 ref 時或 out 參數和其他具有值參數,如下列範例所示,。

    class RefOverloadExample
    {
        public void SampleMethod(int i) { }
        public void SampleMethod(ref int i) { }
    }

在要求相符,例如隱藏或覆寫, ref 和 out 簽章的其他案例中是簽章的一部分,並不會彼此對應。

屬性 (Property) 不是變數, 它們是方法,並且不能傳遞給 ref 參數。

如需如何傳遞陣列的詳細資訊,請參閱使用 ref 和 out 傳遞陣列 (C# 程式設計手冊)

您無法使用下列幾種方法使用 ref 和 out 關鍵字:

  • 非同步方法,或是使用 非同步 修飾詞,請定義。

  • Iterator 方法,其中包括 yield return 或 yield break 陳述式。

範例

上述範例示範發生的事情,當您將實值型別參考。 您也可以使用 ref 關鍵字傳遞參考型別。 傳遞參考型別 (Reference Type) 使呼叫的方法修改參考參數所參考的物件。 物件的儲存位置傳遞至方法做為參考參數傳遞的值。 如果您變更參數的儲存位置,您變更基礎引數的儲存位置。 下列範例會參考型別的執行個體做為 ref 參數。 如需如何透過參考型別的詳細資訊實值和參考,請參閱 傳遞參考型別的參數 (C# 程式設計手冊)

class RefExample2
{
    static void ChangeByReference(ref Product itemRef)
    {
        // The following line changes the address that is stored in  
        // parameter itemRef. Because itemRef is a ref parameter, the
        // address that is stored in variable item in Main also is changed.
        itemRef = new Product("Stapler", 99999);

        // You can change the value of one of the properties of
        // itemRef. The change happens to item in Main as well.
        itemRef.ItemID = 12345;
    }

    static void Main()
    {
        // Declare an instance of Product and display its initial values.
        Product item = new Product("Fasteners", 54321);
        System.Console.WriteLine("Original values in Main.  Name: {0}, ID: {1}\n",
            item.ItemName, item.ItemID);

        // Send item to ChangeByReference as a ref argument.
        ChangeByReference(ref item);
        System.Console.WriteLine("Back in Main.  Name: {0}, ID: {1}\n",
            item.ItemName, item.ItemID);
    }
}

class Product
{
    public Product(string name, int newID)
    {
        ItemName = name;
        ItemID = newID;
    }

    public string ItemName { get; set; }
    public int ItemID { get; set; }
}

// Output: 
//Original values in Main.  Name: Fasteners, ID: 54321

//Back in Main.  Name: Stapler, ID: 12345

C# 語言規格

如需詳細資訊,請參閱 C# 語言規格。語言規格是 C# 語法和用法的限定來源。

請參閱

參考

傳遞參數 (C# 程式設計手冊)

方法參數 (C# 參考)

C# 關鍵字

概念

C# 程式設計手冊

其他資源

C# 參考