C# Extension Methods

This Tutorial Will Explain All About C# Extension Methods. You will Learn to Work with C# extension methods Including their specifications and benefits.

C# Extension Methods

What are the C# Extension Methods?

C# Extension methods are static methods, as the name suggests, are additional methods. Extension methods allow you to inject additional methods without modifying, deriving, or recompiling the original class. This feature is available in  C# 3.0 compilers and further versions ( c# 4.0); this feature is very important for all developers especially if you would like to use the dynamism of the C# enhancements to take place in your class design.

Why do we need it ?

While designing classes in .NET projects, we used to have sibling classes that do simple operation from its parent classes to add a kind of customization for parents’ methods. Suppose that we want to extend int class in .NET by adding a factorial method to it using recursion technique.

What is the specification of Extension method?

An extension method is a special kind of static method that allows you to add new methods to existing types without creating derived types.
C# Extension methods make it possible to write a method to a class that doesn’t offer the method at first. You can also add a method to any class that implements a specific interface, so multiple classes can make use of the same implementation.

e.g.

public static class StringExtension
{
    public static string ExtToUpper(this string name)
    {
           return name.toUpper();
    }
}

An extension method is declared in a static class. An extension method is defined as a static method where the first parameter defines the type it extends.

The ExtToUpper () method extends the string class, as is defined with the first parameter. For differentiating extension methods from normal static methods, the extension method also requires ‘this’ keyword with the first parameter. Indeed, it is now possible to use the ExtToUpper () with the string type.

string name = "avinash";
string newString = name.ExtToUpper();

Console.WriteLine(newString);


Output: AVINASH

This might appear to be breaking object -” oriented rules because a new method is defined for a type without changing the type. However, this is not the case. The extension method cannot access private members of the type it extends. Calling an extension method is just a new syntax of invoking a static method and With the string, you can get the same result by calling the method ExtToUpper () this way:

string name = "Avinash";
string newString = StringExtension.ExtToUpper(name);
Console.WriteLine(newString);


Output: AVINASH

To invoke the static method, write the class name followed by the method name. Extension methods are a different way to invoke static methods. If the static method is defined then you don’t have to supply the name of the class. Instead, the static method is taken because of the parameter type. You just have to import the namespace that contains the class to get the —- extension method in the scope of the String class.

Benefits of extension methods

  • This feature is important for all C# developers, especially if you would like to use the dynamism of the C# enhancements in your class’s design.
  • If the class is sealed then there is no concept of extending its functionality. For this, a new concept i.e, extension method is introduced.
  • Extension methods allow us to extend existing classes without relying on inheritance and no change is required in the class’s source code.

Note:  namespace required for extension method is System.Linq 

Conclusion

In this article, we learned that C# Extension methods are additional custom methods that were originally not included with the class.

The first parameter of the extension method must be of the type for which the extension method is applicable, preceded by the this keyword.

C# Extension methods can be used anywhere in the application by including the namespace of the extension method.

Leave a Comment

error

Enjoy this blog? Please spread the word :)

RSS
Follow by Email
YouTube
YouTube
LinkedIn
Share