Consider the following program:
class A
{
public static void Foo()
{
}
}
static class Ext
{
public static void Foo(this A a)
{
}
}
class Program
{
static void Main(string[] args)
{
var a = new A();
a.Foo();
}
}
This fails to compile, with the error:
Member ‘Test.A.Foo()’ cannot be accessed with an instance reference; qualify it with a type name instead
Why is the compiler ignoring the extension method?
The problem is overload resolution: The static method
Foo()is a candidate, it is applicable – just choosing it as best match will cause an error – which is exactly what happens. Extension methods are only candidates for overload resolution after all other candidates have been considered. In the case of OPs problem case the extension method will not even have been considered before the error occurs.