Monday, September 21, 2009

Extension methods – great new feature of C# 3.0

C# 3.0 has a new great feature which is extension methods. It basically allows you adding methods to already existing types without recompiling, creating derived types, and yes it can be used with sealed types!

Let's take a look how to create an extended method for string type which checks if string is empty or null.


Ok we have a method defined, let's see how to use it – it couldn't be simpler, just type method name after string object. As you can see Visual Studio IntelliSense shows it as an extension with arrow shape icon.


I think extension methods are great feature making developer life easier and what's important code just looks cleaner and simpler, doesn't it?

No comments: