A tutorial explaining the techniques behind extension methods by implementing a bunch of highly useful C# extensions and static functions.
In .net you have the ability to write extensions to already existing classes and types even if the original code is a third party one and you can’t access the sources directly. In this tutorial I will show you how to implement those extensions method and I will also show my workaround for static extensions because those are still not possible.
To begin with let’s have a lock at the IntelliSense suggestions for the “object” type .

As you can see by default there are 4 methods available. In former times if you want to extend this list you would have to have access to the original “object” implementation to add the functions you desire. Nowadays you can achieve the same goal by using extensions method. For example consider calling the ToString() method while the myObject instance is still “null”. The result will by a NullReferenceException.
Read the rest of this entry »