Extension Methods in C#

Extension Methods are a beautiful feature introduced in C # version 3.0. This article simply helps beginners to understand the power of extension methods and  how to use them in a particle scenario.

As the name suggests it is nothing but a way to "add" methods to existing types without creating a new derived type or modifying the original type. They allow you to extend an existing type with new functionality, without having to sub-class or recompile the old type.

For example below:

public class ProjUtility

{

    public static bool IsNumericResult(string str)

    {

        float output;

        return float.TryParse(str, out output);

    }

}

An extension method is a special kind of static method that allows you to add new methods to existing types without creating derived types. The extension methods are called as if they were instance methods from the extended type.

Sample Code

namespace ExtensionMethods

{

    public class maths

    {

        public int number1 { get; set; }

        public int number2 { get; set; }

        public int Add(int num1, int num2)

        {

            return num1 + num2;

        }

    }

    public static class MathsExtension

    {

        public static int substract(this maths mth, int num1, int num2)

        {

            return (num2 - num1);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            int result = 0;

            int extResult = 0;

            maths math = new maths();

            math.number1 = 100;

            math.number2 = 200;

            int num1 = math.number1;

            int num2 = math.number2;

            result = math.Add(num1, num2);

            extResult = math.substract(num1, num2);

            Console.WriteLine("Result is " + result);

            Console.WriteLine("Extenstion methods Result is " + extResult);

            Console.ReadLine();

        }

    }

}

In the preceding we have a maths class with one method, Add(), which adds two numbers and produces a result. Now a new requirement has come and we need to add one more method, subtract. We can fullfill the requirement by adding one more method to the Maths class but what if we don’t have the source code available or we have to add a class without changing the original class, then an extension method is useful.

If we look at the extension class closely, then we can see that this class is static in nature and has a static method substract() with a parameter of the class maths which is needed to get extension by adding one more method.

public static class MathsExtension

{

    public static int substract(this maths mth, int num1, int num2)

    {

        return (num2 - num1);

    }

}

 

That method will be available with instances of class without making any change with the down arrow sign to use it.


Important Sticky
  • Extension methods should be a member of a static class and should be static also.
  • Extension methods cannot be used to override existing methods.
  • This keyword must be the first parameter in the extension method parameter list
  • Extension methods can't access the private methods in the extended type
  • IF you create extension methods that have the same signature methods inside the type you are extending, then the extension methods will never be called.
Ebook Download
View all
Learn
View all