How to call extension method which has the same name as an existing method? [duplicate]

Solution 1:

You can't call the extension method as a normal extension method. The instance method overrides the extension method with the same signature

EDIT:

You can call it as a static method

ExtensionTest.MethodA(a);

Solution 2:

You can't call it as an extension method. It's basically useless at this point, in terms of being an extension method. (Personally I'd like this to be a warning, but never mind.)

The compiler tries all possible instance methods before it attempts to resolve extension methods. From section 7.6.5.2 of the C# 4 spec:

In a method invocation of one of the forms [...] if the normal processing f the invocation finds no applicable methods, an attempt is made to process the construct as an extension method invociation.

and later:

The preceding rules mean that instance methods take precedence over extension methods

You can call it like a regular static method though:

// Fixed typo in name
ExtensionTest.MethodA(a);

Solution 3:

Extension Methods - MSDN

An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.

You can call the extension method as regular static method of a class.

ExtenstionTest.MethodA(a);

From the MSDN

In other words, if a type has a method named Process(int i), and you have an extension method with the same signature, the compiler will always bind to the instance method. When the compiler encounters a method invocation, it first looks for a match in the type's instance methods. If no match is found, it will search for any extension methods that are defined for the type, and bind to the first extension method that it finds. The following example demonstrates how the compiler determines which extension method or instance method to bind to.

Solution 4:

You can call extension method as any other static method:

ExtenstionTest.MethodA(a);