Compiler Warning (level 2) CS0279

'type name' does not implement the 'pattern name' pattern. 'method name' is either static or not public.

There are several statements in C# that rely on defined patterns, such as foreach and using. For example, foreach relies on the collection class implementing the enumerable pattern. 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

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  
    }  
}