Extension Methods

Introduced in C# 3.0 as part of the Language Integrated Query (LINQ)-enabling language feature set, extension methods provide a means to extend an existing type (such as a class, struct, interface, enum, delegate) with additional methods that can be invoked as if they were instance methods. Basically, they provide user convenience for calling helper methods.

Prior to .NET 3.5 (which carried C# 3.0 and VB 9.0, both with extension methods as a language feature), it was rather cumbersome to define additional functionality on an existing type if you didn’t own that type. For example, assume you got some urgent need for a method to reverse a string. Wouldn’t it be great to be able to write the next piece of code to do so?

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.