6

Throughout this site I commonly see people answer questions with such answers as "it works like that because the compiler replaces [thing] with [other thing]", so my question is, how do people know/learn this? Where can I learn these things?

MetaGuru
  • 42,847
  • 67
  • 188
  • 294
  • 11
    You need to know the secret handshake. – ChaosPandion Dec 14 '11 at 18:46
  • http://www.dotnetperls.com/compiler – D Stanley Dec 14 '11 at 18:46
  • 3
    [C# Language Specification](http://msdn.microsoft.com/en-us/library/ms228593.aspx) and [Reflectors](http://stackoverflow.com/questions/2425973/open-source-alternatives-to-reflector) – Tim Schmelter Dec 14 '11 at 18:49
  • 1
    ildasm will let you learn a lot about what the C# compiler produces, as you easily experiment and see what IL it emits. You might also check out Roslyn, whose API reveals the perspective from which the compiler sees C# code. http://blogs.msdn.com/b/csharpfaq/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx – Dan Bryant Dec 14 '11 at 18:50

5 Answers5

13

The most definitive source for how the C# compiler interprets code is the C# language spec.

Also the following blogs provide a lot of more insight into the C# language. Mandatory reading for anyone who wants to become an expert in the language

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 3
    @GSerg I often think of the C# lang spec as "what the compiler is doing" and Eric's blog as "why the compiler is doing what it's doing" – JaredPar Dec 14 '11 at 18:49
2

One technique is to compile your code, and then decompile it using tools such as ILSpy. Using such a tool, you can view the raw IL and see for yourself what the compiler produces.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
1

In addition to the other answers, I'd like to mention that LINQPad is my favorite tool for inspecting IL for quick snippets.

You can type a snippet of code, and immediately see the IL.
It's by far the easiest tool to use, and you can make changes and see the results instantly.

Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
0

In addition to checking the Intermediate Language and reading the language specification, please allow me to add "CLR via C#" by Jeffrey Richter. Microsoft Press Library of Congress Control Number: 2009943026. This reference is amazing, and goes into complete detail on what's happening under the covers.

Gayot Fow
  • 8,710
  • 1
  • 35
  • 48
-1

Niklaus Wirth's book Compiler Construction (PDF) is an introduction to the theory and the techniques of compiler construction. It gives you a general idea of what a compiler is and what it does.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188