Writing C # 's Hello World with the IDE, such as Visual Studio, is easy, but out of the IDE can you print Hello world? This is not to say that you are working out of the IDE, but instead learn about the CLR's execution model.
Hello World
1, create a new Notepad, enter the following code, save as HelloWorld.txt.
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args) {
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
2. Open the Visual Studio 2008 (2005,2010) command prompt
3. Switch to the HelloWorld.txt directory
4. Run command: Csc/out:hello.exe HelloWorld.txt
If no accident, will compile the Hello.exe, can print out Hello world.
CLR execution model-compile time
The execution process of a CLR program is broadly divided into two steps, the compile period and the run time, and the process of compiling is roughly as follows:
The compile-time logic can also be divided into two steps:
1. The CLR (C #) compiler accepts source code files and compiles them into managed modules. Managed modules include IL code, metadata, CLR prime components. In the example above, the HelloWorld.txt is compiled into a managed module.
2, the General Assembly will contain many source code files (here only HelloWorld.txt) and resource files, the second step is to combine the individual source code files and resource files corresponding to the compilation results of the assembly.
Perform the above two steps to get a XX.dll or XX.exe assembly, just like the Hello.exe above.
How does the compiler know whether to compile to a managed module or a resource file? In fact, you must explicitly tell the compiler how to compile each file, which corresponds to the build operation of the Visual Studio file properties.
Right-click any Visual Studio solution for the resource schema---Properties--and build actions:
Specifying CLASS1 as an embedded resource, using Ilspy view, will find just embedding Class1 in the assembly, named: namespace. File name:
You can even set a picture to compile so the compiler tries to compile it, but it will give an error.
Operating period
The assembly is generated above and the IL code in the assembly is not yet a code that can be run. Il is a CPU-independent machine language that is compiled into native code (CPU instructions) by the JIT (Just-in-time, real-time) compiler until the assembly is called. At run time, the CLR performs the following steps:
1, check the security characteristics of the Assembly;
2, allocating space in memory;
3. Send the executable code in the assembly to the JIT compiler, and compile the cost machine code (CPU instruction) in part.
The executable code of the assembly is compiled by the JIT compiler when needed, and then the native code (CPU instructions) is cached for later execution in the program. Once the application terminates, the compiled native code is discarded as well.
For example, if you change the above code to:
static void Main(string[] args) {
Console.WriteLine("Hello");
Console.WriteLine("World!");
Console.ReadKey();
}
The first WriteLine needs to be JIT-compiled before executing. Because of the compiled WriteLine code, the second WriteLine executes the code in the memory block directly, skipping JIT compilation.
Due to the allocation of memory, JIT compilation process, and so on, so the program will cause some performance loss during the first run, write the net when the feeling is very obvious, according to the F5 will wait a long time to display the first page.
The following simulation feels the process. Use a whole bunch of classes to extend memory allocation time, refer to this file HelloWorld.cs:
Run the command again: Csc/out:hello.exe HelloWorld.txt, Get Hello.exe, execute when found a certain delay will print out Hello world.
Raw-Cost machine code
Use. NET provides the NGen.exe, the IL code can compile the cost machine code, can solve the above problem. NGen.exe has two functions:
1. Speed up application start-up. Because the code is compiled into native code, the runtime does not need to spend more time compiling.
2. Reduce the application's assembly. If a single assembly loads multiple processes at the same time, NGen.exe compiles the IL into a separate file. This allows the code to be shared and to avoid a single piece of code for each process by mapping to multiple processes in a "memory-mapped" manner.
Run the Visual Studio 2008 (2005,2010) command prompt again
Run the following command: NGen install Hello.exe:
After the command is complete (about 10 seconds after my machine is ready to enter the command again), running Hello.exe will find that Hello World is ready to print without any delay.