15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

272 ❘ ChaPTer 11 lAnGuAGe inteGrAted Query<br />

It is important to note that the variable query just has the LINQ query assigned to<br />

it. The query is not performed by this assignment but rather as soon as the query is<br />

accessed using the foreach loop. This is discussed in more detail later in the section<br />

“ Deferred Query Execution.”<br />

extension methods<br />

The compiler modifi es the LINQ query to invoke methods instead. LINQ offers various extension methods<br />

for the IEnumerable < T > interface so you can use the LINQ query across any collection that implements<br />

this interface.<br />

Extension methods make it possible to write a method to a class that doesn ’ t offer the method at fi rst. You<br />

can also add a method to any class that implements a specifi c interface, so multiple classes can make use of<br />

the same implementation.<br />

For example, wouldn ’ t you like to have a Foo() method with the String class The String class is<br />

sealed, so it is not possible to inherit from this class. You can do an extension method, as shown in the<br />

following code:<br />

public static class StringExtension<br />

{<br />

public static void Foo(this string s)<br />

{<br />

Console.WriteLine("Foo invoked for {0}", s);<br />

}<br />

}<br />

An extension method is defi ned as a static method where the fi rst parameter defi nes the type it extends<br />

<strong>and</strong> it is declared in a static class. The Foo() method extends the string class, as is defi ned with the fi rst<br />

parameter. For differentiating extension methods from normal static methods, the extension method also<br />

requires the this keyword with the fi rst parameter.<br />

Indeed, it is now possible to use the Foo() method with the string type:<br />

string s = "Hello";<br />

s.Foo();<br />

The result shows Foo invoked for Hello in the console, because Hello is the string passed to the<br />

Foo() method.<br />

This might appear to be breaking object - oriented rules because a new method is defi ned for a type<br />

without changing the type or deriving from it. However, this is not the case. The extension method<br />

cannot access private members of the type it extends. Calling an extension method is just a new syntax<br />

of invoking a static method. With the string you can get the same result by calling the method Foo()<br />

this way:<br />

string s = "Hello";<br />

StringExtension.Foo(s);<br />

To invoke the static method, write the class name followed by the method name. Extension methods are<br />

a different way to invoke static methods. You don ’ t have to supply the name of the class where the static<br />

method is defi ned. Instead, the static method is taken because of the parameter type. You just have to<br />

import the namespace that contains the class to get the Foo() extension method in the scope of the<br />

String class.<br />

One of the classes that defi ne LINQ extension methods is Enumerable in the namespace System.Linq .<br />

You just have to import the namespace to open the scope of the extension methods of this class. A sample<br />

implementation of the Where() extension method is shown in the following code. The fi rst parameter of<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!