13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Changes to type <strong>in</strong>ference and overload resolution245<strong>C#</strong> query code withlambda expressions<strong>C#</strong> query code withlambda expressionsCompile timeIL us<strong>in</strong>gdelegates<strong>C#</strong> compiler<strong>C#</strong> compilerIL us<strong>in</strong>gexpression treesExecution timeQuery resultsDelegate codeexecuted directly<strong>in</strong> the CLRDynamic SQLQuery resultsLINQ to SQL providerExecuted at databaseand fetched backLINQ to ObjectsLINQ to SQLFigure 9.4 Both LINQ to Objects and LINQ to SQL start off with <strong>C#</strong> code, and end with query results.The ability to execute the code remotely comes through expression trees.some of the expression locally and some elsewhere. We’ll look at some of the details ofthis conversion step <strong>in</strong> chapter 12, but you should bear this end goal <strong>in</strong> m<strong>in</strong>d as weexplore extension methods and LINQ syntax <strong>in</strong> chapters 10 and 11.NOTENot all check<strong>in</strong>g can be done by the compiler—When expression trees areexam<strong>in</strong>ed by some sort of converter, there are often cases that have to berejected. For <strong>in</strong>stance, although it’s possible to convert a call tostr<strong>in</strong>g.StartsWith <strong>in</strong>to a similar SQL expression, a call to str<strong>in</strong>g.IsInterned doesn’t make sense <strong>in</strong> a database environment. Expressiontrees allow a large amount of compile-time safety, but the compiler canonly check that the lambda expression can be converted <strong>in</strong>to a validexpression tree; it can’t make sure that the expression tree will be suitablefor its eventual use.That f<strong>in</strong>ishes our direct coverage of lambda expressions and expression trees. Beforewe go any further, however, there are a few changes to <strong>C#</strong> that need some explanation,regard<strong>in</strong>g type <strong>in</strong>ference and how the compiler selects between overloaded methods.9.4 Changes to type <strong>in</strong>ference and overload resolutionThe steps <strong>in</strong>volved <strong>in</strong> type <strong>in</strong>ference and overload resolution have been altered <strong>in</strong><strong>C#</strong> 3 to accommodate lambda expressions and <strong>in</strong>deed to make anonymous methodsmore useful. This doesn’t count as a new feature of <strong>C#</strong> as such, but it can be importantto understand what the compiler is go<strong>in</strong>g to do. If you f<strong>in</strong>d details like thistedious and irrelevant, feel free to skip to the chapter summary—but remember thatthis section exists, so you can read it if you run across a compilation error related tothis topic and can’t understand why your code doesn’t work. (Alternatively, youmight want to come back to this section if you f<strong>in</strong>d your code does compile, but youdon’t th<strong>in</strong>k it should!)Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!