C#

Bring more functionalities to types using extension methods

Hey guys,

What are extension methods ?

Extension methods are quite simple to understand and they can be quite useful in time of needs. An extension method give you the power to create a new method for any type that you would like. They’re static methods designed to extend the capabilities of a type without having to extend the type and add the method(s) that a programmer would like to use.

Extension methods must be defined in a separate static class as static methods, as it was mentioned earlier. Although they’re static methods inside a static class, those methods can be call using instance method syntax(e.g you can use a local variable/ parameter and have access to that extension method). When creating your static class, you need to keep in mind that not only everything must be static when you’re declaring the class and method but the first parameter specifies the type on which the extension method will act. When defining your method, the first parameter must be preceded by the this operator. In a few words, the this operator refers to the current instance of the class or object if you prefer. It enables you, when using this, to access any member to the current instance of your class.

Why use extension methods ?

Why would you need to define extension methods in your normal day life as a developer? Things can happen and sometimes, the library you’re using could be more restrictive than you would like such as the class you would like to extend for a few more functionalities is sealed. In those times, extension methods can be quite useful in order to achieve quickly what you’d like to do.  So here’s below an example of an extension method. What the sample will show you is an extension method with the sole purpose of reversing the value of an int.

Extension method example

 

using System;
//That easy to create your very own extension methods
public static class Int32Extensions
{
    public static int ReverseValue(this int value)
    {
        return -1 * value;
    }
}

public static Main(string[] args)
{
     var value = 10;
     Console.WriteLine(value.ReverseValue());
     Console.ReadLine(); // Will print -10 🙂
}

 

Before I forget, there’s something I would like you to do when designing your extension methods. Beware, it becomes easy to have only one class for every extension method you will use. Don’t. You should separate concerns and make sure to either you one static class per type or at least, regroup extension methods into one domain and make sure the types being extended in your class are quite similar.

 

Kevin out.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s