Extension Methods in .NET

Extension methods in .net are awesome. Ever since I left the ruby development community, I was slightly saddened by the inability to modify classes. In Ruby, you can open up a class anytime and modify it on the fly, even at runtime. Now that I think about this more, being able to modify any class in the system at any time may not be such a good idea.

The .net way, I believe, maybe even better. No, you cannot modify classes during runtime, and you cannot add methods, even extension methods, at runtime. Everything needs to be done at compile time. However, you can add methods to whatever class you want, and I believe this includes overloading existing methods.

During a recent project, I created several extension methods for the string and lists classes. Thinking about it, I believe they were methods that exist in Ruby but were lacking in .net. One of these was a method for the list of string type which would concatenate all your strings with a separator you specified. In Ruby, this is simply called join. I was pretty proud of myself when I created this extension method.

Then, the other day, I found a little site called extensionmethod.net. This site is exactly what I was looking for. It has all sorts of extension methods that people from around the globe have written for any conceivable purpose. The one thing that is lacking is libraries that are available for download and inclusion in your project. However, this is probably a good thing as its far safer to simply download the source and include them into your project yourself.

If you are a .NET developer and you have not checked out extension methods, I highly recommend you do so. They can make a lot of your development easier and less tedious.
(and more fun!)
Until next time, Jason

Posted Wednesday, February 16th, 2011 under Software Development.

Leave a Reply