共用方式為


編譯器錯誤 CS0425

更新:2007 年 11 月

錯誤訊息

方法 'method' 型別參數 'type parameter' 的條件約束,必須與介面方法 'method' 型別參數 'type parameter' 的條件約束相符。請考慮用明確介面實作替代。

如果虛擬的泛型方法在衍生的類別 (Derived Class) 中遭到覆寫,且該衍生類別中方法的條件約束與基底類別中方法的條件約束不相符,便會發生這個錯誤。若要避免這個錯誤,請確定兩個宣告中的 where 子句一致,或明確地實作介面。

範例

下列範例會產生 CS0425:

// CS0425.cs

class C1
{
}

class C2
{
}

interface IBase
{
    void F<ItemType>(ItemType item) where ItemType : C1;
}

class Derived : IBase
{
    public void F<ItemType>(ItemType item) where ItemType : C2  // CS0425
    {
    }
}

class CMain
{
    public static void Main()
    {
    }
}

只要約束條件集具有相同的意義,條件約束的文字不必完全相同。例如,下列範例即為可接受的情況:

// CS0425b.cs

interface J<Z>
{
}

interface I<S>
{
    void F<T>(S s, T t) where T: J<S>, J<int>;
}

class C : I<int>
{
    public void F<X>(int s, X x) where X : J<int>
    {
    }

    public static void Main()
    {
    }
}