C # compiler see Fluent syntax or query expression?

I am always embarrassed about this.

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 read in a book that:

The compiler processes the query expression, translating it into free syntax

But in the reflector, I see the opposite: Reflector code

This is not a fluent syntax.

So what does the compiler see?

+4
source share
5 answers

A β€œcompiler” is, by definition, a device that translates text written in one language into another language.

The C # compiler logically translates C # programs containing query expressions into C # -tout-query expressions, and then translates these programs into IL. (Keep in mind that you don’t really need to do this middle stage of the translation, it should behave as if it did, but if the compilers are smart enough to skip this intermediate step and still get the correct output, we certainly can do it.)

Reflector is also a compiler. It translates IL to C #. How this happens is a business.

You cannot draw any conclusions about what the C # compiler does based on the output of the Reflector; these are completely different programs written by different people to solve various problems.

+18
source

The syntax for understanding a query compiles directly into method calls; they produce indistinguishable IL.

However, many decompilers will always translate LINQ calls into query understanding syntax.
Perhaps you can change this behavior in the parameters.

+6
source

The Stock csc compiler converts any syntax into an IL, freely or otherwise. What you are looking at is the reconstruction chosen by the reflector.

The specific syntax is just sugar for more complex designs under the hood, and expressions are one of these examples . Other examples include foreach loops and lambda expressions .

Now, if it is LINQ to SQL or Entities, the query syntax and free syntax are implemented as expressions and translated by the provider under the hood. This is not the same as in L2O.

+3
source

I believe you are reading this back (but I could be wrong)

What he says is that

 from n in names where n.Contains("a") orderby n.Length select n.ToUpper() 

Translated to

 names .Where(n => n.Contains("a")) .OrderBy(n => n.Length) .Select(n => n.ToUpper()); 
+1
source

I think the book "Free Syntax" means "Delayed Execution" in LINQ chains. When only on the actual data request, the request is created on the basis of all requests executed earlier (if any). All queries made earlier are analyzed and one (if possible) optimized LINQ query to retrieve data.

From the point of view of the final result, like other people, it always falls into the IL instruction set.

0
source

Source: https://habr.com/ru/post/1402138/


All Articles