What Is the Extension Method in C#?

DZone 's Guide to

What Is the Extension Method in C#?

An overview of Extension Methods in C# and a step-by-step instruction on how to write one.

· Web Dev Zone ·
Free Resource

I often get questions about the Extension Method in C#. The Extension Method was introduced in C# Version 3.0. and allows us to add functionalities in an existing class without modifying, extending, or re-compiling it.

Essentially, the Extension Method allows us to add a new method to an existing class:

  • Without modifying it or adding code
  • Without extending it or creating a new derived type
  • Without recompiling the class

Extension methods are a special kind of static method but can be called on objects like an instance method. So an extension method can be used in the same way as normal instance methods.

Steps to Create An Extension Method

Step 1: Define a static visible class which will contain the Extension Method or Extension Methods. Make sure the class is visible to the client code by applying the appropriate accede modifier.

Step 2: Create a static method with at least the same visibility level as the containing class.

Step 3: The first parameter of the Extension Method always specifies the type method operates on. Make sure the type name is preceded with the “this” modifier.

Step 4: In the calling code, add the namespace that contains Extension Method class.

Step 5: Use the Extension Method on the type in the same instance method can be used. Keep in mind that we do not need to pass the first parameter because that denotes the type, however we should pass the second parameter onwards to call the extension method.

Let’s Create An Extension Method!

Let's go ahead and create an Extension method for the String class. The string class does not have any WordCount method. We start by creating a console application project and then add a class to the console application project. We’ll name the class StringExtension.

Once the class is created, we have to perform the steps as discussed earlier:

  1. Make the class static and public in this case.
  2. Add a static method.
  3. Since the class is public, make the method public too.
  4. Pass the first parameter of the static method as the name of the class preceded with keyword “this”.
  5. Write the code inside method to implement functionality:
namespace ExtensionMethodDemo
    public static class StringExtension
        public static int WordCount(this string s)
            int count = 0;
            for (int i = 0; i < s.Length; i++)
                if (s[i] != ' ')
                    if ((i + 1) == s.Length)
                        if (s[i + 1] == ' ')
            return count;

In the above code listing, we have created an Extension Method to count the number of words in a given string. You may notice that:

  1. The StringExtension class is a public static class.
  2. The WordCount method is a public static method.
  3. The first parameter in the WordCount method is a string because we are creating WordCount as an extension method of string class.
  4. The first parameter type string is preceded with the keyword “this”.

 Before using the Extension Method, let us compile the project. To do so, we need to perform the following steps:

  1. Add DLL to client project, if we have created Extension Method in different library.
  2. Add namespace of the Extension Method class with using directive.
  3. Use Extension Method as normal instance method.

We can use the newly created Extension Method as shown in the listing below:

using System;

namespace ExtensionMethodDemo
    class Program
        static void Main(string[] args)
            Console.WriteLine("Enter a senetence");
            string inputstr = Console.ReadLine();
            int numberofword = inputstr.WordCount();

As we notice in the above listing, the Extension Method WordCount is used to count the number of words in a sentence. Visual Studio also shows the Extension Methods in intellcense, by appending the word extension next to the method name as shown in the image below:

As we see, we can use an Extension Method in the same way as any instance method. There could be a scenario where we have an instance method and an Extension Method with the same name and signature. In that case, an instance method always has more priority over the Extension Method. There are a few important points we should keep in mind about the Extension Method:

  1. It has lesser priority than the instance method, so if a class has an Extension Method and an Instance Method with the same name and signature, the instance method has priority.
  2. If an extension method conflicts with a member method of target type, the member method is always invoked instead of the extension method.
  3. The extension method can only access private members of the target type.

Extension Methods are very useful to add functionality in an existing class. Many LINQ functions are implemented as Extension Methods. I hope the information provided here will help you in your projects, and thanks for reading!

c#, extension methods

Published at DZone with permission of Dhanajay Kumar , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}