Extension methods are additional custom methods. It is a new feature introduced in C# 3.0 that allows us to add new methods into a class without changing the source code of the class means if a class consists of a set of members in it and if we want to add new methods into the class, we can add those methods without making any changes to the source code of the class. It does not require modifying, deriving, or recompiling the original class or interface.
Before extension methods, we used inheritance for extending the functionality of a class. In inheritance, we define a child class to that existing class and then we add new members in the child class. But, in the case of an extension method, we will extend the functionality of a class by defining the methods that we want to add into the class in a new class and then binds them to an existing class.
Extension Method Real-time example:
As we know, a string is a built-in class in the .net framework. That means the source code of this class is not available and hence we can change the source code of the string class. Our need is to add a method to the string class i.e GetWordCount() This method will return the number of words present in a string. We can achieve this using the extension method as shown below:
- In the above method, we have created a static class that consists of a static method GetWordCount(). The first parameter in this method is the string class name so that we can call this method on the string class object.
- Now, we need to modify the main method of the Program class as shown below:
In the main method, we have used our extension method on the string class object.
Important points to remember:
There are some key points to remember while using C# extension methods:
- Extension methods must be defined only under the static class.
- Extension method should always be defined as a static method, but when they are bound with any class or structure they will convert into non-static methods.
- 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 that is used for binding.
- An extension method can have only one binding parameter and that should be defined in the first place of the parameter list.
- If required, an extension method can be defined with a normal parameter also starting from the second place of the parameter list.
- Extension methods can be used anywhere in the application by including the namespace of the extension method.
- The extension method does not support method overriding.
- It must be defined in a top-level static class.
- It cannot apply to fields, properties, or events.
- The main advantage of the extension method is to add new methods in the existing class without using inheritance.
- We can add new methods in the existing class without modifying the source code of the existing class.
- It can also be used with sealed classes.
If you have skills in PHP programming and you want to enhance your career in this field, a PHP Certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.