Published on

Extension methods in C#

Authors
  • avatar
    Name
    Cristian Pique
    LinkedIn

Introduction

Extension methods in C# are a way to add new methods to existing classes without modifying the original class or creating a new derived class. They are defined as static methods in a static class, but they are called as if they were instance methods on the extended class.

The signature of an extension method includes the "this" keyword followed by the type of the class that is being extended, the return type, the name of the method, and its parameters.

Here is an example of an extension method that adds a "doubleValue" method to the int class:

public static class IntExtensions {
    public static int DoubleValue(this int value) {
        return value * 2;
    }
}

This extension method can be called like this:

int number = 5;
int result = number.DoubleValue(); //result is 10

Extension methods are useful for adding functionality to existing classes without modifying the original class or creating a new derived class. This can make the code more flexible and easier to maintain. For example, a class that works with dates can have an extension method that calculates the number of days between two dates.

public static class DateTimeExtensions {
    public static int DaysBetween(this DateTime startDate, DateTime endDate) {
        return (endDate - startDate).Days;
    }
}

Pros & cons

However, it is important to use extension methods with caution, as they can make the code harder to understand and maintain. Since they are not part of the original class, they can make the code harder to understand, especially for developers who are not familiar with the extension method. Also, if you are extending a class that belongs to a third party library, you will have to create a new class and define the method in it, which can make the code harder to maintain.

Advantages of extension methods are that they can add functionality to existing classes without modifying the original class or creating a new derived class, and they can make the code more flexible and easier to maintain. Disadvantages are that they can make the code harder to understand and maintain, especially for developers who are not familiar with the extension method.

How to

To create an extension method in C#, we need to define a static method in a static class, and use the "this" keyword followed by the type of the class that is being extended. Also, the method should be defined as public, and the static class should be defined as public too. Then, whenever you want to use it, you need to make sure to include a reference to the project or to the corresponding namespace to have intellisense on the object you want to extend.

Summary

Extension methods in C# are a way to add new methods to existing classes without modifying the original class or creating a new derived class. They can be useful for adding functionality to existing classes, but they can also make the code harder to understand and maintain. It is important to use them with caution and only when it is appropriate.