DEV Community

99darshan
99darshan

Posted on

Introduction to Extension Methods in C#

Introduction

Extension Methods, as the name suggests, are methods used to extend the functionality of any C# types without having to directly modify and re-compile the original type itself.

General format of Extension methods in C#

    public static class ClassName
    {
        public static ReturnType ExtensionMethodName(this TypeToWhichExtMethodWillBeAdded t)
        {
            // Extension Method definition
        }
    }
Enter fullscreen mode Exit fullscreen mode

Use Case

Extension methods are typically used whenever there is a need to add certain functionality to any C# types which are available as an external library or ".dll". In cases where the source code of external library is unavailable or restricted for modifications, C# extension methods can be used to extend the functionality of certain class or struct provided in that external library.

Example Scenario

Consider the following scenario, C# has an inbuilt Class Random in System Namespace which can be used to generate random numbers. Next() method defined in Random class generates a random number.

Now, let's say we want to generate random English alphabet instead of random number. However, there is no in-built method in the Random class which allows us to do that. And since Random is an inbuilt type provided by C#, we can not directly modify the class itself and add a new method to generate random alphabet.

Random number generation using inbuilt method in Random Class

    using System;

    namespace ExtensionMethodDemo
    {
        class Program
        {
            static void Main(string[] args)
            {   
                Random rnd = new Random();
            // Next() method returns a random integer
                int randomNum = rnd.Next(); 
                Console.WriteLine("random Num " + randomNum);
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

Output: random Num 1854662654

Adding NextAlphabet() extension method to the Random Class

We can create an Extension Method named NextAlphabet() and use it as if it was defined in the Random class itself.

using System;

namespace ExtensionMethodDemo
{
   public static class ExtensionMethods
   {
      // Extension Method which return random english letter
      public static char NextAlphabet(this Random rnd)
      {
         int rndIndex = rnd.Next(0, 26);
         return (char)('a' + rndIndex);
      }
    }
}
Enter fullscreen mode Exit fullscreen mode

Using NextAlphabet() Extension Method:

    using System; 
    namespace ExtensionMethodDemo
    {
        class Program
        {
            static void Main(string[] args)
            {   
                Random rnd = new Random();
            // Next() method returns a random integer
                int randomNum = rnd.Next(); 
                Console.WriteLine("random Num " + randomNum);

            // EXTENSION METHOD NextAlphabet()
                // Extension method are called exactly like instance method
                char randomLetter = rnd.NextAlphabet();
                Console.WriteLine("Random Letter: " + randomLetter);
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

Output:

random Num 1066262963
Random Letter: q
Enter fullscreen mode Exit fullscreen mode

Points to note about C# extension methods

  • C# extension methods must be defined in a static class. In the example above, all extension methods are inside static class ExtensionMethods.
  • C# extension methods must be defined as static.
  • Unlike regular static method in C#, extension method must have the first parameter preceded by "this" keyword. The first parameter with "this" keyword specifies the type for which the extension method is being added. In the example below, we are adding NextAlphabet() extension method to the type of System.Random class.
    // Note the static and this Keyword in Extension Method Signature
    public static char NextAlphabet(this Random rnd)
    {
        // ...
    }
Enter fullscreen mode Exit fullscreen mode
  • Even though extension methods are defined as static method, they are invoked exactly like an instance method. An instance of that type needs to be created before calling extension methods. Since first parameter with the "this" keyword specifies the type for which extension method is defined, it shouldn't be passed as an argument when calling the extension method.
    // Calling an extension method
    Random rnd = new Random();
    // invoked like any other instance method of Random Class
    char randomLetter = rnd.NextAlphabet();
Enter fullscreen mode Exit fullscreen mode

Extension method with additional parameter

Any additional parameters to the extension methods must be added to the parameter list after the first parameter.

Now let's create an extension method for Random Class which returns a random item from any given list passed as an argument.

    public static string PickRandom(this Random rnd, List<string> list)
    {
        int rndIndex = rnd.Next(0, list.Count);
        return list[rndIndex];
    }
Enter fullscreen mode Exit fullscreen mode

Using PickRandom() Extension Method

    using System;
    using System.Collections.Generic;
    namespace ExtensionMethodDemo
    {
        class Program
        {
            static void Main(string[] args)
            {   
            Random rnd = new Random();
                List<string> names = new List<string> { "Ronaldo", "Hazard", "Messi", "Lampard" };
                // list names is passed as an argument to the extension method
            string randomName = rnd.PickRandom(names);
                Console.WriteLine("Random Name: " + randomName);
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

Output: Random Name: Lampard

Identifying extension methods in visual studio

Visual studio has a separate icon to distinguish normal methods from extension methods. Extension Methods are identified by an icon with a little downward arrow to the right of pink cube.

Extension Method Icon in Visual Studio

Additional notes about C# extension methods

  • Extension methods can be applied to any types including value types, however, they can't be applied to fields or properties.
  • When creating an extension method make sure that a method with same name and method signature doesn't exist in the original type itself. If the method name and method signature of extension method matches an instance method in the original type, then the instance method will be invoked instead of extension method.

Conclusion

  • C# extension method are useful to add functionality to types where the modifications to the original source code can't be made.
  • Since extension methods eliminate the need to create a new derived type in our project, it improves the readability of our code.

Top comments (0)