C # Compiled for CIL

I understand the following C # code:

var evens = from n in nums where n % 2 == 0 select n; 

compiles:

 var evens = nums.Where(n => n % 2 == 0); 

But what does it mean that it compiles? Do I have the impression that C # code compiles directly to CIL?

+6
source share
2 answers

I think you misunderstood something. Request Expression:

 var evens = from n in nums where n % 2 == 0 select n; 

not compiling:

 var evens = nums.Where(n => n % 2 == 0); 

Rather, two lines of code are compiled directly into CIL. It so happens that they compile (efficiently) an identical CIL. The compiler can convert the request into an intermediate form during the analysis of the request code, but the end result is, of course, CIL.

+2
source

This is a C # / LINQ expression:

var evens = from n in nums where n % 2 == 0 select n;

This is a C # lambda expression:

var evens = nums.Where(n => % 2 == 0);

They are both C # and both are compiled into CIL.

You can learn more about lambdas here:

Read more about LINQ here:

Two expressions are equivalent.

One does not get โ€œcompiledโ€ to the other. Honest:)

+1
source

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


All Articles