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:

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.