Extension methods

Extension methods allow programmers to add new methods to an existing type without having to create an inherited class or to recompile the original. Thus, you can add methods to classes for which you might not even have the sources (e.g., System.String). An extension method is defined the same way as any other, with two stipulations:

â?¢ It is declared as static in an outer-level static, non generic class.
â?¢ The type it is extending is declared as the first parameter, preceded by this.

The method can then be called as an instance method on an object of the type that has been extended.

reference. C# Language Specification Version 3.0, September 2007, Section 10.6.9

Leave a Reply