共用方式為


編譯器警告 (層級 2) CS0279

更新:2007 年 11 月

錯誤訊息

'type name' 未實作 'pattern name' 模式,因為 'method name' 是靜態或者不是公用的。

C# 中有數種陳述式需依賴定義的模式,例如 foreach 和 using。例如,foreach 依賴實作可列舉模式的集合類別。當編譯器因方法宣告為 static 或不為 public 而無法進行比對時,便會發生這個錯誤。模式中的方法須為類別的執行個體,而且是公用的 (Public)。

範例

下列範例會產生 CS0279:

// CS0279.cs

using System;
using System.Collections;

public class myTest : IEnumerable
{
    IEnumerator IEnumerable.GetEnumerator()
    {
        yield return 0;
    }

    internal IEnumerator GetEnumerator()
    {
        yield return 0;
    }

    public static void Main()
    {
        foreach (int i in new myTest()) {}  // CS0279
    }
}