Extension methods in C#

Extension methods in C# enable us to add a new method to an existing type. This is done without creating a new derived type or recompiling, or modifying the original type. In this tutorial let us learn how to create an Extension method in C#

Extension methods

Consider the class

Invoke the above from your form using

This will show “Hello” in the message box.

Consider the scenario where you don’t have the access to the above code and you want to add another method to the above class.  That is where the C# feature of an extension method is to be used.

Extension methods in C#

Extension Methods
Extension Methods

Extension methods

Extension methods as the name implies is about extending the functionalities of the class.  The Extension methods help you add a new method to the existing Class (or Type).

Now let us add the new method sayHI to the above testclass .  First, create a new static class and name it as testExtension.  Remember that the extension method must be defined as static  method

Extension Methods in C#
Extension Methods in C#

Now you can use sayHI method  just like any other method as shown below.

Few Important things to note

  1. An extension method must be defined as a static method and must reside in a static class
  2. this keyword must be the first parameter of the extended method. This will actually inform the compiler that the method is an extension method.
  3. An extension method must be defined in the root of the namespace. It cannot be nested inside another class under the namespace
  4. The type that is being extended must be referenced in the extension method. Use the using statement to import the namespace.
  5. protected/private members of the type that is being extended will not be accessible from the Extension methods.
  6. You cannot override any method of the type you are extending.
  7. The Extension method has lower priority than the methods defined In the type.  A similarly named method in an instance, always executed rather than the extended method. In fact, the extended method will never be called
  8. The Extension methods with the same name and signature may signature for the same class may be declared in multiple namespaces without causing compilation errors.
  9. Extension method cannot be used for adding a new properties, fields or events

Benefits

  1. Extension methods appear under the IntelliSense.
  2. It makes the code more readable.
  3. Extend the functionality of third party libraries where you don’t have access to the code. ( This may also break your code if the third party vendor changes the implementation of the library)

Conclusion

Extension methods are the nice little feature of c#.  LINQ is one module which uses extension methods extensively.

Further reading

Extension Methods (C# Programming Guide)

Extension methods best practices extension methods

Source Code

Download

1 thought on “Extension methods in C#”

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top