編譯器警告 (層級 2) CS0280
更新:2007 年 11 月
錯誤訊息
'type' 未實作 'pattern name' 模式。'method name' 的簽章錯誤。
C# 的兩個陳述式 foreach 和 using,分別倚賴預先定義的模式 "collection" 與 "resource"。當方法有錯誤的簽章,而使編譯器無法讓任一陳述式與其模式相符時,便會產生這個警告。例如,"collection" 模式需要一個名為 MoveNext 的方法,此方法不需使用參數,而會傳回 boolean。您的程式碼可能包含 MoveNext 方法,但有參數或可能傳回物件。
另一個範例為 "resource" 模式與 using。"resource" 模式需要 Dispose 方法,如果以相同名稱定義屬性,便會產生這個警告。
若要解決這個警告,請確認型別中的方法簽章與模式中對應方法的簽章相同,並確認屬性名稱與模式所需方法的名稱不同。
範例
下列範例會產生 CS0280。
// CS0280.cs
using System;
using System.Collections;
public class ValidBase: IEnumerable
{
IEnumerator IEnumerable.GetEnumerator()
{
yield return 0;
}
internal IEnumerator GetEnumerator()
{
yield return 0;
}
}
class Derived : ValidBase
{
// field, not method
new public int GetEnumerator;
}
public class Test
{
public static void Main()
{
foreach (int i in new Derived()) {} // CS0280
}
}