编译器警告(等级 2)CS0279Compiler Warning (level 2) CS0279

“type name”不实现“pattern name”模式。'type name' does not implement the 'pattern name' pattern. “method name”是静态的或非公共的。'method name' is either static or not public.

C# 中的多条语句依赖于定义的模式,例如 foreachusingThere are several statements in C# that rely on defined patterns, such as foreach and using. 例如, foreach 依赖于实现可枚举模式的集合类。For example, foreach relies on the collection class implementing the enumerable pattern. 由于被声明为 static 或未被声明为 public的方法,编译器不能建立匹配时,将发生该错误。This error occurs when the compiler is unable to make the match due to a method being declared static or not public. 模式中的方法必须是类的实例,并且必须是公共的。Methods in patterns are required to be instances of classes, and to be public.

示例Example

下面的示例生成 CS0279:The following example generates 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  
    }  
}