Reputation: 26971
Let's say you write an application in C#, VB.NET, anything with .NET. When you hit build, does it really compile your code? I thought so until I started using Redgate's reflector on some of my assemblies and saw my code verbatim. I would have expected loops to be unrolled and another plethora of optimizations, instead nothing.
So when does the compilation actually happen? I think when it is built, code become CIL (Intermediary Language) and when execution occurs, it is loading in the CLR? Is it optimized during CLR only and never at build time?
Upvotes: 9
Views: 7460
Reputation: 144112
It is compiled down to CIL at, well, compile time. Reflector's magic is that it "understands" the CIL and converts it back into C# (or VB.NET or whatever. Look under the Options menu in Reflector and you can view the assembly in any format, including the CIL).
In Reflector, you actually are not seeing your original code. You are seeing a translation of the CIL into C#. Most of the time that will be very similar to what you wrote, but there are some telltale signs - for example, find a place where you implemented an auto-property:
string MyProperty {get;set;}
And you'll see what that actually compiles to, which is something like this:
public string MyProperty
{
[CompilerGenerated]
get
{
return this.<MyProperty>k__BackingField;
}
[CompilerGenerated]
set
{
this.<MyProperty>k__BackingField = value;
}
}
Upvotes: 4
Reputation: 52123
When you compile in VS
When you execute:
I post the answer here too as the other question was not really about this...
Upvotes: 25