C# Extension Methods

I’ve been talking about Object-Oriented Principles for a while, and this post will not be different. Here and here, I’ve introduced OOP and described an elegant solution to comply with Open-Closed Principle using Visitor Pattern, respectively. Today I’ll be talking about a C# 3.0 feature called extension methods. Maybe you’re now asking what extension methods and OOP have in common. That is what this post is about.

Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

Therefore, extension methods are an optional technique to extend classes functionalities without modifying it. This makes me remember the Open-Closed Principle which states that software entities should be open for extension, but closed for modification. By using extension methods, your software tends to be less difficult to change, to not impact in dependent modules and even more important, changes are achieved by adding new code, not by changing old code that already exists.

Extension methods are defined as static methods in static classes, but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. The following example shows extension methods for C# string class.

StringExtension.cs

em1

Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive. They are used as if they were instance methods and are visualized in VS intellisense, as show below:

em2

Here is the test class for StringExtension.cs class showing how to invoke extension methods in C#.

TestExtensionMethods.cs

em3

As you could see, this feature enables you to extend the string class, which is a sealed class, same as final class in Java, and implossible to inherit it. This helps you to extend and maintain code in a really elegant way. So, remeber using extension methods when find similar problems in you code.

See you,


Fernando

3 thoughts on “C# Extension Methods

  1. You covered how extension methods can help you be compliant with the OCP in regard to other classes, but what about extending the extensions themselves? (1) They’re required to be in static classes — which the OCP forbids you to modify when you need to add new extension methods. (2) If another type exposes a method with the same signature, the caller is required to use the extension method’s class (e.g., StringExtension.BooleanValue), so type naming is important. (3) It seems inelegant to have a few extension methods for, say, the string type in one class (e.g., StringExtension), and your new ones in another class (e.g., StringExtension2).

    To this end, extension methods resemble decorators to me, in that each one should be in its own class (StringToBoolExtension, StringToIntExtension, etc.).

    Thoughts?

    1. Yeah!

      Extensions methods are really useful when working with third party APIs, since you can add methods to existing classes that can not be inherited.

      In an object-oriented perspective, they really don’t add value. Actually, extension methods don’t provide an environment for OCP. They are the workaround for programmers when this environment does not exist.

      Other advantage in using them is readability. Instead of coding StringExtensions.IntValue(str), you can now just say str.IntValue(). Much more easy to read. Remember that source code is written only once, but read 100 times.

      Right?

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s