共用方式為


out (C# 參考)

您可以在兩個內容中 (每個都是詳細資訊的連結),使用 out 內容關鍵字,做為參數修飾詞,或在介面和委派的泛型類型參數宣告中,使用該關鍵字。 本主題探討參數修飾詞,但您可以查看這個其他主題,以取得泛型類型參數宣告的相關資訊。

out 關鍵字會導致引數由參考傳遞。 這類似於 ref 關鍵字,只是 ref 需要在傳遞之前,先初始化變數。 若要使用 out 參數,方法定義和呼叫方法都必須明確地使用 out 關鍵字。 例如:

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}

雖然做為 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 OutOverloadExample
{
    public void SampleMethod(int i) { }
    public void SampleMethod(out int i) { i = 5; }
}

屬性不是變數,因此無法做為 out 參數傳遞。

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

您不能將 ref 和 out 關鍵字用於下列幾種方法:

  • 使用 async 修飾詞定義的非同步方法。

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

範例

當您想要方法傳回多個值時,宣告 out 方法很有用。 下列範例使用 out,在單一方法呼叫中,傳回三個變數。 請注意,第三個引數指派為 null。 這可讓方法能選擇性地傳回值。

    class OutReturnExample
    {
        static void Method(out int i, out string s1, out string s2)
        {
            i = 44;
            s1 = "I've been returned";
            s2 = null;
        }
        static void Main()
        {
            int value;
            string str1, str2;
            Method(out value, out str1, out str2);
            // value is now 44 
            // str1 is now "I've been returned" 
            // str2 is (still) null;
        }
    }

請參閱

其他資源

C# 參考