Share via


擴充方法 (C# 程式設計手冊)

擴充方法可讓您在現有類型中「加入」方法,而不需要建立新的衍生類型、重新編譯,或是修改原始類型。 擴充方法是一種特殊的靜態方法,但是會將它們當成擴充類型上的執行個體方法來呼叫。 對於以 C# 和 Visual Basic 撰寫的用戶端程式碼,呼叫擴充方法或是在類型中實際定義的方法,兩者之間並沒有明顯的差別。

最常見的擴充方法是 LINQ 標準查詢運算子,這些運算子會將查詢功能加入至現有的 IEnumerableIEnumerable 類型。 若要使用標準查詢運算子,請先使用 using System.Linq 指示詞將它們帶入範圍內。 接著,任何實作 IEnumerable 的類型都會具有執行個體方法,如 GroupBy``2OrderBy``2Average 等。 如果在 IEnumerable 類型 (如 ListArray) 的執行個體後面輸入「點」,就可以在 IntelliSense 陳述式完成功能中看到這些額外的方法。

下列範例將示範如何在整數陣列上呼叫標準查詢運算子 OrderBy 方法。 括號括住的運算式就是 Lambda 運算式。 許多標準查詢運算子會將 Lambda 運算式當成參數,但是擴充方法不會強制這樣做。 如需詳細資訊,請參閱 Lambda 運算式 (C# 程式設計手冊)

class ExtensionMethods2    
{

    static void Main()
    {            
        int[] ints = { 10, 45, 15, 39, 21, 26 };
        var result = ints.OrderBy(g => g);
        foreach (var i in result)
        {
            System.Console.Write(i + " ");
        }           
    }        
}
//Output: 10 15 21 26 39 45

擴充方法會定義為靜態方法,但透過執行個體方法語法呼叫。 擴充方法的第一個參數會指定方法作業所在的類型,而且參數前面會加上 this 修飾詞。 您必須使用 using 指示詞將命名空間明確匯入至原始程式碼,擴充方法才會進入範圍中。

下列範例將示範針對 String 類別定義的擴充方法。 請注意,擴充方法是定義在非巢狀且非泛型的靜態類別內:

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' }, 
                             StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }   
}

使用這個 using 指示詞就可以將 WordCount 擴充方法帶入範圍中:

using ExtensionMethods;

而使用下列語法,就可以從應用程式中呼叫它:

string s = "Hello Extension Methods";
int i = s.WordCount();

在您的程式碼中,可以利用執行個體方法語法來叫用擴充方法。 不過,編譯器所產生的中繼語言 (IL) 會將您的程式碼轉譯為靜態方法上的呼叫。 因此,實際上並未違反封裝 (Encapsulation) 的準則。 事實上,擴充方法無法存取它們所擴充之類型中的私用變數。

如需詳細資訊,請參閱 如何:實作和呼叫自訂擴充方法 (C# 程式設計手冊)

一般而言,您呼叫擴充方法的頻率將遠高於實作自己的方法。 因為擴充方法是使用執行個體方法語法進行呼叫,所以不需要任何特殊知識就可以從用戶端程式碼使用它們。 若要啟用特定類型的擴充方法,只要針對定義這些方法所在的命名空間加入 using 指示詞即可。 例如,若要使用標準查詢運算子,請將下面這個 using 指示詞加入至程式碼:

using System.Linq;

(您可能也要加入對 System.Core.dll 的參考)。您將會注意到,標準查詢運算子現在出現在 IntelliSense 中,做為適用於大部分 IEnumerable 類型的額外方法。

注意事項注意事項

雖然標準查詢運算子未針對 String 出現在 IntelliSense 中,但是您仍然可以使用它們。

在編譯時期繫結擴充方法

您可以使用擴充方法來擴充類別或介面,但無法覆寫它們。 而且永遠不會呼叫擁有與介面或類別方法相同名稱和簽章的擴充方法。 在編譯時期,擴充方法的優先順序一律低於類型本身中定義的執行個體方法。 換句話說,如果類型具有名為 Process(int i) 的方法,而您的擴充方法也具有相同的簽章,則編譯器一律會繫結至執行個體方法。 編譯器遇到方法引動過程時,會先在類型的執行個體方法中尋找相符項目。 如果找不到相符項目,則會搜尋任何針對該類型定義的擴充方法,並繫結至找到的第一個擴充方法。 下列範例將示範編譯器如何判斷要繫結的擴充方法或執行個體方法。

範例

下列範例將示範 C# 編譯器遵循的規則,用以判斷要將方法呼叫繫結至類型上的執行個體方法,還是繫結至擴充方法。 靜態類別 Extensions 包含針對任何實作 IMyInterface 之類型定義的擴充方法。 類別 A、B 和 C 都會實作這個介面。

因為 MethodB 擴充方法的名稱和簽章與這些類別已實作的方法完全相同,所以絕不會呼叫該方法。

當編譯器找不到具有相符簽章的執行個體方法時,就會繫結至相符的擴充方法 (如果有的話)。

// Define an interface named IMyInterface. 
namespace DefineIMyInterface
{
    using System;

    public interface IMyInterface
    {
        // Any class that implements IMyInterface must define a method 
        // that matches the following signature. 
        void MethodB();
    }
}


// Define extension methods for IMyInterface. 
namespace Extensions
{
    using System;
    using DefineIMyInterface;

    // The following extension methods can be accessed by instances of any  
    // class that implements IMyInterface. 
    public static class Extension
    {
        public static void MethodA(this IMyInterface myInterface, int i)
        {
            Console.WriteLine
                ("Extension.MethodA(this IMyInterface myInterface, int i)");
        }

        public static void MethodA(this IMyInterface myInterface, string s)
        {
            Console.WriteLine
                ("Extension.MethodA(this IMyInterface myInterface, string s)");
        }

        // This method is never called in ExtensionMethodsDemo1, because each  
        // of the three classes A, B, and C implements a method named MethodB 
        // that has a matching signature. 
        public static void MethodB(this IMyInterface myInterface)
        {
            Console.WriteLine
                ("Extension.MethodB(this IMyInterface myInterface)");
        }
    }
}


// Define three classes that implement IMyInterface, and then use them to test 
// the extension methods. 
namespace ExtensionMethodsDemo1
{
    using System;
    using Extensions;
    using DefineIMyInterface;

    class A : IMyInterface
    {
        public void MethodB() { Console.WriteLine("A.MethodB()"); }
    }

    class B : IMyInterface
    {
        public void MethodB() { Console.WriteLine("B.MethodB()"); }
        public void MethodA(int i) { Console.WriteLine("B.MethodA(int i)"); }
    }

    class C : IMyInterface
    {
        public void MethodB() { Console.WriteLine("C.MethodB()"); }
        public void MethodA(object obj)
        {
            Console.WriteLine("C.MethodA(object obj)");
        }
    }

    class ExtMethodDemo
    {
        static void Main(string[] args)
        {
            // Declare an instance of class A, class B, and class C.
            A a = new A();
            B b = new B();
            C c = new C();

            // For a, b, and c, call the following methods: 
            //      -- MethodA with an int argument 
            //      -- MethodA with a string argument 
            //      -- MethodB with no argument. 

            // A contains no MethodA, so each call to MethodA resolves to  
            // the extension method that has a matching signature.
            a.MethodA(1);           // Extension.MethodA(object, int)
            a.MethodA("hello");     // Extension.MethodA(object, string) 

            // A has a method that matches the signature of the following call 
            // to MethodB.
            a.MethodB();            // A.MethodB() 

            // B has methods that match the signatures of the following 
            // method calls.
            b.MethodA(1);           // B.MethodA(int)
            b.MethodB();            // B.MethodB() 

            // B has no matching method for the following call, but  
            // class Extension does.
            b.MethodA("hello");     // Extension.MethodA(object, string) 

            // C contains an instance method that matches each of the following 
            // method calls.
            c.MethodA(1);           // C.MethodA(object)
            c.MethodA("hello");     // C.MethodA(object)
            c.MethodB();            // C.MethodB()
        }
    }
}
/* Output:
    Extension.MethodA(this IMyInterface myInterface, int i)
    Extension.MethodA(this IMyInterface myInterface, string s)
    A.MethodB()
    B.MethodA(int i)
    B.MethodB()
    Extension.MethodA(this IMyInterface myInterface, string s)
    C.MethodA(object obj)
    C.MethodA(object obj)
    C.MethodB()
 */

一般方針

一般而言,建議您應謹慎地實作擴充方法,而且只有在必要時才實作。 當用戶端程式碼必須擴充現有的類型時,應該盡可能以建立衍生自現有類型的新類型來達成此目的。 如需詳細資訊,請參閱繼承 (C# 程式設計手冊)

使用擴充方法來擴充無法變更其原始程式碼的類型時,會有類型實作的變更導致擴充方法中斷的風險。

如果您要實作所指定類型的擴充方法,請記住下列幾點:

  • 如果擴充方法的簽章與類型中定義的方法相同,則絕不會呼叫擴充方法。

  • 擴充方法是帶入命名空間層級的範圍。 例如,如果有多個靜態類別在名為 Extensions 的單一命名空間中包含擴充方法,則 using Extensions; 指示詞會將這些擴充方法全都帶入範圍中。

針對實作的類別庫,您不應該使用擴充方法阻止組件的版本號碼遞增。 如果您要在擁有其原始程式碼的程式庫中加入重要功能,則應遵循組件版本控制的標準 .NET Framework 方針。 如需詳細資訊,請參閱組件版本控制

請參閱

參考

Lambda 運算式 (C# 程式設計手冊)

概念

C# 程式設計手冊

標準查詢運算子概觀

其他資源

平行程式設計範例 (包括許多範例擴充方法)

執行個體參數的轉換規則與其影響

語言之間擴充方法的互通性

擴充方法和局部調用委派

擴充方法繫結和錯誤報告