C Sharp Programming/Extension methods

Extension methods are a feature new to C# 3.0 and allow you to extend existing types with your own methods. While they are static, they are used as if they are normal methods of the class being extended. Thus, new functionality can be added to an existing class without a need to change or recompile the class itself. However, since they are not directly part of the class, extensions cannot access private or protected methods, properties, or fields.

Extension methods should be created inside a class. They themselves should be and should contain at least one parameter, the first preceded by the  keyword:

The type of the first parameter (in this case ) specifies the type with which the extension method will be available. You can now call the extension method like this:

Here is the rest of the program:

Note that extension methods can take parameters simply by defining more than one parameter without the keyword.