Using Extension Methods

You’ve already seen how to use an extension method simply by using the first parameter as the left side (mimicking instance method invocation syntax), but another question is how those extension methods become available. The answer is to be found in the use of namespaces, which are covered in Chapter 24, “Namespaces.” In short, a namespace provides a means to group types in a hierarchical manner. For example, the System.Text namespace contains types like StringBuilder, while System.IO contains types that deal with files, streams, and so on. Instead of having to specify full type names all over the place, you can import a namespace using the using keyword in C#:

using System;class Program

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.