Book Image

Instant .NET 4.5 Extension Methods How-to

By : Shawn Ricardo Mclean
Book Image

Instant .NET 4.5 Extension Methods How-to

By: Shawn Ricardo Mclean

Overview of this book

.NET extension methods is an essential feature to know and understand for all .NET developers. Usage of extension methods is found in applications ranging from small to large scale enterprise systems built using the .NET framework. Create and use extension methods the correct way to save your development time and maintainability costs. Instant .NET 4.5 Extension Methods How-to is a practical, hands-on guide that provides you with a number of clear, step-by-step exercises that will help you take advantage of the real power that is behind extension methods and gives you good knowledge of how to use them in your .NET applications. This book covers how to create, write, and use different types of extension methods. It will take you through a number of clear, practical recipes that will help you take advantage of the power of extension methods in the quickest possible way. You will also learn exactly how to create extension methods on strings, interfaces, classes such as IQueryable and IEnumerable, and so on. You will write them from scratch and then use them practically in your application. You will also learn the most suitable scenarios for using these extension methods.You will learn everything you need to know about creating your own extension methods and using them and other external extension methods.
Table of Contents (6 chapters)

Overriding extension methods (Should know)


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.

Getting ready

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.

How to do it...

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());
            }
        }
    }
}

How it works...

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.