Byyo
Byyo

Reputation: 2243

Extension Method overloading

I have this extension method

public static class Extensions
{
     public static void ConsoleWriteLine(this object Value)
     {
         Console.WriteLine(Value);
     }
}

for integer values i have a little modification

public static void ConsoleWriteLine(this int Value)
{
    Console.WriteLine("Integer: " + Value);
}

my question is: when i write int x = 1; x.ConsoleWriteLine(); what makes the decision to take the second extension in this case? int is a object aswell

Upvotes: 2

Views: 1237

Answers (3)

D Stanley
D Stanley

Reputation: 152556

what makes the decision to take the second extension in this case?

When the compiler has multiple valid methods to choose from, it uses a set of overload resolution rules to determine which method it should bind to. The second extension method matches the calling signature exactly, so it is chosen. Since any other type is directly convertible to object, the first extension would be chosen. Other numeric types are implicitly convertible to int, but an implicit conversion is not "better" than a direct conversion to a parent class.

I believe the relevant spec here is 7.5.3.2:

7.5.3.2 Better function member

For the purposes of determining the better function member, a stripped-down argument list A is constructed containing just the argument expressions themselves in the order they appear in the original argument list. Parameter lists for each of the candidate function members are constructed in the following way:

...

Given an argument list A with a set of argument expressions { E1, E2, ..., EN } and two applicable function members MP and MQ with parameter types { P1, P2, ..., PN } and { Q1, Q2, ..., QN }, MP is defined to be a better function member than MQ if

  • for each argument, the implicit conversion from EX to QX is not better than the implicit conversion from EX to PX, and

Since the "conversion" from int to int is "better" that the conversion from int to object, the int overload is chosen.

Note that this applies to all overloads, not just extension methods (although there are different rules for breaking the tie between extension and non-extension methods).

Upvotes: 5

Matias Cicero
Matias Cicero

Reputation: 26281

According to MSDN:

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 compiler searches from bottom to top (that is, from child class to parent class, until it reaches the top of the class hierarchy), so in this case, the most specific method will always win.

Upvotes: 2

Christoph K
Christoph K

Reputation: 354

Well, in this case:

public static class Extensions
{
 public static void ConsoleWriteLine(this object Value)
 {
     Console.WriteLine(Value);
 }
}

You're writing an extension method for an object. See:

this object Value

In your other extension method, you're writing an extension for an integer.

public static void ConsoleWriteLine(this int Value)
{
   Console.WriteLine("Integer: " + Value);
}

Because

this int Value

So, everytime you declare an Integer, this extension method will be called, because this is exactly a method for an integer and not for an object.

Upvotes: 0

Related Questions