Extension methods cannot be overridden the way classes and instance methods are. They are overridden by a slight trick in how the compiler selects which extension method to use by using "closeness" of the method to the caller via namespaces. In this recipe, you will learn how to manipulate the namespace to get the correct extension method you wish to use.
Refer to the StringExtensions.cs
file in the ExtensionMethods.Library
project for the extension methods. These methods are used in the Program.cs
file in the ExtensionMethods.Console
project.
The following code shows two extension methods, Shuffle
, in two separate namespaces:
namespace ExtensionMethods.Library { public static class StringExtensions { public static string Shuffle(this string value) { char[] array = value.ToCharArray(); Random rnd = new Random(); return string.Concat(array.OrderBy(x => rnd.Next())); } } } namespace UnknownExtensions { public static class StringExtensions { public static string Shuffle(this string value) { char[] array = value.ToCharArray(); Random rng = new Random(); int n = array.Length; while (n > 1) { n--; int k = rng.Next(n + 1); var val = array[k]; array[k] = array[n]; array[n] = val; } return new string(array); } } }
The following code shows the use of the extension methods:
namespace ExtensionMethods.Console { using ExtensionMethods.Library; internal class Program { private static void Main(string[] args) { DoTaskSix(); MyNamespace.Program.DoTaskSixUpgraded(); } private static void DoTaskSix() { System.Console.WriteLine("inefficient shuffle".Shuffle()); } } namespace MyNamespace { using UnknownExtensions; internal static class Program { public static void DoTaskSixUpgraded() { System.Console.WriteLine("efficient shuffle".Shuffle()); } } } }
In the code snippet, we have two extension methods, one in our typical ExtensionMethods.Library
namespace and one in a new UnknownExtensions
namespace. Both extensions are given the same name, but are implemented differently.
In the code snippet where we used these methods, we called the extension method normally in DoTaskSix
where the using ExtensionMethods.Library;
line is directly applied to the caller. To call the new extension method added outside of our normal namespace, we had to declare a new namespace (MyNamespace
) and include using UnknownExtensions
for that scope.
Name spacing: You can change which extension method is called by ensuring that the namespace it is in is closer, or in the same namespace of the caller. We can also make the compiler select the extension directly by the
using
keyword as seen in the preceding code snippet. However, if we have an extension method in both namespaces that are being used in the same scope, we will end up with an ambiguous method error from the compiler:using ExtensionMethods.Library; using UnknownExtensions;
Avoid creating extension methods in the same namespace as other instance methods or extension methods, as future updates might implement a method with the same signature and break your code.
Instance methods: Instance methods cannot be overridden. An extension method with the same signature as an instance method will, however, compile without errors but will not take precedence over the instance method during compilation. The
GetHashCode
method is one such example that cannot be overridden. Remember, extension methods are just the compiler facilitating static methods.
In this recipe, you have learned how to implement and use overridden extension methods. No form of this is recommended, unless it is completely required after redesigning or refactoring your code.