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
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for