Author - StudySection Post Views - 1,209 views
Extension

Extension Methods in C#

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:
csharp

  1. 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.
  2. Now, we need to modify the main method of the Program class as shown below:
    csharp1
    In the main method, we have used our extension method on the string class object.
    Expected Output:
    csharp2

Important points to remember:

There are some key points to remember while using C# extension methods:

  1. Extension methods must be defined only under the static class.
  2. 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.
  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 that is used for binding.
  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.
  6. Extension methods can be used anywhere in the application by including the namespace of the extension method.
  7. The extension method does not support method overriding.
  8. It must be defined in a top-level static class.
  9. It cannot apply to fields, properties, or events.

Advantages:

  1. The main advantage of the extension method is to add new methods in the existing class without using inheritance.
  2. We can add new methods in the existing class without modifying the source code of the existing class.
  3. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri