Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compiler sees Fluent syntax or Query Expression?

Tags:

c#

linq

.net-4.0

I'm always confusing about this stuff.

I have this query :

string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };

IEnumerable<string> query = names.Where(n => n.Contains("a"))
.OrderBy(n => n.Length)
.Select(n => n.ToUpper());

I've read in a book that:

The compiler processes a query expression by translating it into fluent syntax

But in Reflector I see the opposite: Reflector Code

This is not fluent syntax.

So what does the compiler sees?

like image 346
Royi Namir Avatar asked Nov 27 '22 11:11

Royi Namir


1 Answers

A "compiler" is by definition a device which translates a text written in one language into another language.

The C# compiler logically translates C# programs that contain query expressions into C#-without-query-expressions, and then translates those programs into IL. (Note that it need not actually do that middle stage of translation; it must behave as though it does so, but if the compiler writers are clever enough to skip that intermediate step and still get the right output then we certainly can do so.)

Reflector is also a compiler. It translates IL into C#. How it does so is it's business.

You can't make any conclusions about what the C# compiler does based on the output of Reflector; they are completely different programs written by different people to solve different problems.

like image 126
Eric Lippert Avatar answered Dec 19 '22 01:12

Eric Lippert