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:

static class StringExtensions{    public static string Substring(this ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.