30

I have code like

public class TestA
{
    public string ColA { get; set; }
    public string ColB { get; set; }
    public string ColC { get; set; }
    public void MethodA()
    {
        MessageBox.Show("Original A1.");
    }
}

static class ExtenstionTest
{
  
    public static void MethodA(this TestA A1)
    {
        MessageBox.Show("Extended A1.");
    }
}

Now if I call MethodA like

TestA a = new TestA();
        a.MethodA();

It will always call Original method. How can I call the extension method.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
D J
  • 6,908
  • 13
  • 43
  • 75
  • Also a slight variation http://stackoverflow.com/questions/2303885/if-an-extension-method-has-the-same-signature-as-a-method-in-the-sealed-class-w?rq=1 –  Nov 23 '12 at 07:25

5 Answers5

47

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);
Mihai
  • 2,740
  • 31
  • 45
  • 3
    that wont solve my problem. but thanks for answer.. – D J Nov 23 '12 at 07:03
  • 1
    You're welcome. The idea is that if you want to use the extension method as a genuine extension method, you can't use it if you have an instance method with the same signature because that instance method will always have priority over the extension method :) – Mihai Nov 23 '12 at 07:04
  • 1
    +1 for being the first correct answer, Surprised to see it getting a downvote – Habib Nov 23 '12 at 07:26
  • 1
    I think I know who down voted it :P – Mihai Nov 23 '12 at 07:33
19

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);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
8

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.

Habib
  • 219,104
  • 29
  • 407
  • 436
3

You can call extension method as any other static method:

ExtenstionTest.MethodA(a);
Rafal
  • 12,391
  • 32
  • 54
0

as others suggested you can't call this method.the only way of calling it is such:

TestA a = new TestA();
ExtenstionTest.MethodA(a);
Behnam Esmaili
  • 5,835
  • 6
  • 32
  • 63