How to view C # syntax sugar compiler output

I want to see if there is a method or tool to see how C # compilers are created under the hood, for example, closures or query expressions. I noticed that many blog posts dedicated to these issues will have syntax sugar source code and C # base code that converts the compiler to. So, for example, with linq and query expressions they will show:

var query = from x in myList select x.ToString(); 

then the resulting code will be

 var query = myList.Select(x=>x.ToString()); 

Is this possible with the tool or do you just need to know how it works from the specification and go from there?

+4
source share
4 answers

I am not a Resharper user, but I am sure it will work. It seems you are already familiar with the alternative form of your Linq syntax query. I think this is the answer; Know what you are doing. Read it. If you don’t know, you are dangerous and you won’t go anywhere near my code base. :)

Seriously, this requires a minimum of reading and provides you tremendous power in C #. If you are interested in your work at all, you know that.

+3
source

Resharper can do this conversion (LINQ expression syntax for lambda syntax) is very easy for you.

LINQPad has a tab that can show you the syntax of the lambda expression for the query that you enter into it, and it has another tab that parses it down to the IL code level. (There is another tab that shows you the SQL that is generated if you use LINQ to SQL or LINQ for Entities).

+4
source

How far under the hood do you want to go?

If you are really interested in knowing what your code looks like after compiling it, you should check ildasm.exe . This tool will show you the cold, hard, IL generated when compiling your application. This tool will allow you to open any of your prefabricated assemblies to view the real nuts and bolts that are under the hood.

+1
source

SharpLab.io is the tool you are looking for.

The SharpLab site shows the intermediate steps and results of compiling the code. It allows you to see the code as the compiler sees it, and better understand .NET languages.

0
source

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


All Articles