Do not use IDE To write C # Hello World,

Source: Internet
Author: User

Do not use IDE To write C # Hello World,

Using Visual Studio and other IDE To write C # Hello World is very simple, but can you print Hello World out of IDE? This is not to say that we are away from IDE at work, but to learn the CLR execution model.

Hello World

If you accidentally translate hello.exe, you can print out Hello World.

CLR execution model-compilation period

The execution process of the CLR program is roughly divided into two steps: the compilation period and the runtime period. The compilation period is roughly as follows:


The compilation logic can also be divided into two steps:

You can get a xx.dllor xx.exeset in two steps, just like your hello.exe.

How does the compiler know whether to compile it into a hosted module or a resource file? In fact, it is necessary to clearly tell the compiler how to compile each file, which corresponds to the file attribute generation operation of Visual Studio.

Right-click the file of any Visual Studio solution resource solution --> properties --> Generate operation:


If you specify Class1 as an embedded resource and use ILSpy to view it, you will find that you only embed Class1 into the Assembly named namespace. File Name:


You can even set an image to compile and let the compiler try to compile it, but an error is reported.

Runtime

The Assembly is generated above. The IL code in the Assembly is not runable code. IL is a machine language unrelated to the CPU. It is compiled by the JIT (Just-in-Time, real-Time) compiler into the local code (CPU command) until the Assembly is called ). During running, CLR performs the following steps:

The executable code of the Assembly is compiled by the JIT compiler as needed, and the local code (CPU instruction) is cached for later program execution. Once the application is terminated, the compiled local code is also discarded.

For example, if you change the above Code:

static void Main(string[] args) {
    Console.WriteLine("Hello");
    Console.WriteLine("World!");
    Console.ReadKey();
}

The first WriteLine needs to be compiled by JIT before execution. Because the WriteLine code has been compiled, the second WriteLine will directly execute the code in the memory block and skip JIT compilation.

Due to memory allocation, JIT compilation, and so on, the program will cause some performance loss during the first running, write ASP. NET, and press F5 to display the homepage for a long time.

The process is simulated below. Use a lot of classes to extend the memory allocation time. Refer to this file HelloWorld. cs (blog does not support the txt format ):


Run the command again: csc/out: Hello.exe helloworld.txtto get to Hello.exe. a certain delay occurs during execution to print out Hello World.

Generate local code

Use .netw.ngen.exe to compile the IL code to solve the above problems. NGen.exe has two functions:

Run the Visual Studio 2008 () command again to prompt the program

Run the following command: ngen install Hello.exe:


Command completion (in my machine, it will take about 10 seconds to complete. After the command is input again, run hello.exe and you will find that the Hello World can be printed immediately without any delay.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.