共用方式為


編譯器警告 (層級 1) CS1956

更新:2007 年 11 月

錯誤訊息

成員 'name' 實作型別 'type' 中的介面成員 'name'。在執行階段發現多個相符的介面成員。實作將視所呼叫的方法而定。

當兩個介面方法僅因特定的參數為 ref 或 out 而有所區別時,便會產生這個警告。您最好要變更程式碼以避免這個警告,因為實際上會在執行階段時呼叫哪個方法並不明顯,也無法保證。

雖然 C# 會區分 out 與 ref,但對 CLR 來說,這兩者都是相同的。在決定由何種方法實作介面時,CLR 只會挑選其中一個方法。

若要避免這個警告

  • 請讓編譯器能以某種方式來區別這些方法。例如,您可以為這些方法指定不同名稱,或是為其中一個方法提供額外的參數。

範例

因為在 Base 中的兩個 Test 方法僅因其第一個參數的 ref/out 修飾詞 (Modifier) 而有所不同,下列程式碼會產生 CS1956:

// cs1956.cs
class Base<T, S>
{
    // This is the method that should be called.
    public virtual int Test(out T x) // CS1956
    {
        x = default(T);
        return 0;
    }

    // This is the "last" method and is the one that ends up being called
    public virtual int Test(ref S x)
    {
        return 1;
    }
}

interface IFace
{
    int Test(out int x);
}

class Derived : Base<int, int>, IFace
{
    static int Main()
    {
        IFace x = new Derived();
        int y;
        return x.Test(out y);
    }
}