How to Resolve Warning "CS0279: 'type' does not implement the 'pattern' pattern. 'method' is either static or not public." in C#
The Compiler Warning CS0279 is a pattern matching validity warning. The message reads: "'MyType' does not implement the 'collection' pattern. 'GetEnumerator' is either static or not public."
In C#, features like foreach, await foreach, and using rely on Pattern Matching (Duck Typing). Instead of forcing you to implement a specific interface (like IEnumerable), the compiler simply looks for a method with a specific name (like GetEnumerator). However, for the pattern to match, that method must be accessible (public) and it must be an instance method (not static).
This guide explains why this warning breaks your loops and how to fix the method signature.
Understanding Pattern Requirements
When you write foreach (var x in myObj), the compiler searches myObj for a method with this exact signature:
public IteratorType GetEnumerator()
It ignores methods that are:
- Private / Protected / Internal: The
foreachloop (often running in a different context) cannot access them. - Static: The
foreachloop needs to iterate over this specific instance of data, so a static method (which belongs to the class) makes no sense in this context.
If the compiler finds a method named GetEnumerator but it fails these checks, it issues CS0279 and refuses to compile the loop.
Scenario 1: The Private Iterator
This is the most common cause. You defined the method, but you forgot the access modifier (defaulting to private) or explicitly made it protected.
Example of error:
using System.Collections;
public class DataStore
{
private int[] _data = { 1, 2, 3 };
// ⛔️ Warning CS0279: 'DataStore' does not implement the 'collection' pattern.
// 'GetEnumerator' is not public.
IEnumerator GetEnumerator() // Default is private!
{
return _data.GetEnumerator();
}
}
public class Program
{
static void Main()
{
var store = new DataStore();
// The loop fails because it can't reach the private method
foreach (var i in store) { }
}
}
Scenario 2: The Static Iterator
Sometimes developers define GetEnumerator as static by mistake, perhaps thinking of it as a factory method or a utility. However, iteration is inherently tied to a specific object's state.
Example of error:
using System.Collections;
public class ConfigList
{
// ⛔️ Warning CS0279: 'ConfigList' does not implement the 'collection' pattern.
// 'GetEnumerator' is static.
public static IEnumerator GetEnumerator()
{
return null;
}
}
public class Program
{
static void Main()
{
var config = new ConfigList();
// The loop fails because it expects an instance method
foreach (var c in config) { }
}
}
Solution: Correct the Signature
To make the class compatible with foreach, you must ensure the method is Public and Instance (non-static).
Solution:
using System.Collections;
public class DataStore
{
private int[] _data = { 1, 2, 3 };
// ✅ Correct: Added 'public' and ensured it is not 'static'
public IEnumerator GetEnumerator()
{
return _data.GetEnumerator();
}
}
public class Program
{
static void Main()
{
var store = new DataStore();
// Now works perfectly
foreach (var i in store)
{
System.Console.WriteLine(i);
}
}
}
Output:
1
2
3
Explicit Interface Implementation:
If you want to hide GetEnumerator from the public API but still allow foreach, implementing IEnumerable explicitly (IEnumerator IEnumerable.GetEnumerator()) is valid. The foreach loop detects that the class implements the interface and uses it, bypassing the pattern matching rules for public methods.
Conclusion
CS0279 is the compiler telling you: "I found the method you want, but I'm not allowed to use it."
- Check Visibility: Add
publicto yourGetEnumerator,MoveNext, orDispose(forusing) methods. - Check Scope: Remove
static. These methods must operate on an object instance.