O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Overload Resolution

Actually, the fact that two Reverse extension methods show up illustrates how overload resolution applies to extension methods, too. When we write the following code, the compiler considers the extension method on string to be a better match than the one that’s defined on IEnumerable<T>, with T substituted for char:

var input = Console.ReadLine();Console.WriteLine(input.Reverse());

Hence, our method will take precedence over the one defined by the LINQ APIs. Also, extension methods are considered only when no suitable instance method is found. In other words, they are performed as a “last-chance lookup” operation after all attempts to find instance methods fail:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required