Using Extension methods- Improving readability.

Published on Oct 17, 2008

I consider extension methods one of the most interesting features of the .Net framework. I like it so much because It brings some of the strength of JavaScript into the framework. The possibility of write stuff like this.

An do stuff like this:

There are a lot of discussions about when to use extension methods and when not to. The problem is when you use extension methods instead of inheritance.

Using extension methods to augment the framework or modify the language.

A clear choice for me is when you want to augment types, like the previous example of using PercentOf in an integer.Or what about this.

Or what about having a handy method to get the Unix Time right from the DateTime object?

Or the other way around

Encapsulate functionality that you always do.

I need to use Reflection quite a lot for one of my projects and I have a set of methods to do some very common operations, like get a value from a property, get a list of properties, etc. What if those methods where available for you in every object?

And what about some handy string methods

Have fun!!!

Some of the utilities methods like Squeeze are part of the Subsonic Utilities library that I modified and turned into extension methods.