Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

Linq: Extension Methods Resolution

by the3factory 3/25/2008 7:11:00 AM

Extension Methods Resolution

Extension methods resolution is one of the most important concepts to understand if you want to master LINQ. Consider the following code. In it, we define a custom list of type Customer, called Customers, and a class, CustomersEnumerable, that provides an extension method, called Where, which applies specifically to instances of the Customers type:

public sealed class Customers: List<Customer> {
public Customers(IEnumerable<Customer> items): base(items) {}
}
public static class CustomersEnumerable {
public static IEnumerable<Customer> Where(
this Customers source, Func<Customer, bool> predicate) { ... }
public static IEnumerable<Customer> Where(
this Customers source, Func<Customer, int, bool> predicate) { ... }
}

If we use our usual customers array, the behavior of the query in Listing 4-56 is quite interesting.

Listing 4-56: A query expression over a custom list of type Customers
Image from book

Customers customersList = new Customers(customers);
var expr =
from   c in customersList
where  c.City == "Brescia"
select c;
foreach (var item in expr) {
Console.WriteLine(item);
}
Image from book

The query expression will be converted by the compiler into the following code, as we saw early in this chapter:

var expr =
customersList
    .Where(c => c.City == "Brescia")
.Select(c => c);

As a result of the presence of the CustomersEnumerable class, the extension method Where will be the one defined by CustomersEnumerable, instead of the general-purpose one defined in System.Linq.Enumerable. (To be considered as an extension method container class, the CustomersSequence class must be in the current namespace or in any namespace included in active using directives.) Now we are experiencing the real power of LINQ. Using extension methods, we are able to define custom behaviors for specific types. In the following chapters, we will discuss LINQ to SQL, LINQ to XML, and other implementations of LINQ. These implementations are just specific implementations of query operators, thanks to the extension methods resolution realized by the compilers.

At this time, everything looks fine, and it is, of course! By the way, imagine that you need to query the custom list of type Customers with the standard Where extension method rather than with the specialized one. You should convert the custom list to a more generalized one to divert the extension method resolution made by the compiler. This is another scenario that can benefit from conversion operators.

Tags:

Linq

Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0