Extension Methods in C#

Extension Methods  allows us to add new methods into a class without editing the source code of the class.

Extension methods can be used as an approach to extending the functionality of a class in the future if the source code of the class is not available or we don’t have any permission in making changes to the class.

Before extension methods, inheritance is an approach that used for extending the functionality of a class i.e. if we want to add any new members into an existing class without making a modification to the class, we will define a child class to that existing class and then we add new members in the child class.

In the case of an extension method, we will extend the functionality of a class by defining the methods, we want to add into the class in a new class and then bind them to an existing class.


Points to Remember while working with C# Extension methods:

1. Extension methods must be defined only under the static class. 

2. As an extension method is defined under a static class, compulsory that the method should be         defined  as static whereas once the method is bound with another class, the method changes into non-     static.

3. The first parameter of an extension method is known as the binding parameter which should be the     name of the class to which the method has to be bound and the binding parameter should be prefixed   with this keyword.

4. An extension method can have only one binding parameter and that should be defined in the first   place of the parameter list.

5. If required, an extension method can be defined with a normal parameter also starting from the   second place of the parameter list.