Do not use the IDE to write C # 's Hello World method

Source: Internet
Author: User

Do not use the IDE to write C # 's Hello World method

Submission: Lijiao font: [Increase decrease] Type: Reprint time: 2015-10-15 I want to comment

This article mainly introduces the use of the IDE to write C # 's Hello World method, the need for friends can refer to the next

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

Create a new Notepad, enter the following code, and save as HelloWorld.txt.

?
1234567891011 using System;  namespaceHelloWorld    classProgram        static voidMain(string[] args) {        Console.WriteLine("Hello World!");        Console.ReadKey();        }

Open the Visual Studio 2008 (2005,2010) command prompt


But changed to the HelloWorld.txt directory.


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:

The CLR (C #) compiler accepts source code files and compiles them into managed modules. The managed modules include IL code, metadata, and CLR prime components. The example above is to compile the HelloWorld.txt into a managed module.

The General Assembly will contain many source code files (only HelloWorld.txt) and resource files, and the second step is to merge each source code file and resource file corresponding to the compilation result into an assembly.

You can get a XX.dll or XX.exe assembly in two steps above. 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. The JIT (Just-in-time, real-time) compiler compiles to native code (CPU instructions) until the assembly is called. At run time, the CLR performs the following steps:

Check the security characteristics of the Assembly;

allocating space in memory;

The executable code in the assembly is sent to the JIT compiler, which compiles some of the cost machine code (CPU instructions).

The executable code of the assembly is compiled by JIT compilation 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:

?
12345 staticvoidMain(string[] args) {   Console.WriteLine("Hello");   Console.WriteLine("World!");   Console.ReadKey(); }

The first WriteLine needs to be JIT-compiled before executing. The second WriteLine executes the code in the memory block directly, skipping JIT compilation, because the WriteLine code is compiled.

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 F5 will wait a long time to display the first page.

The following simulation feels the process. Use a bunch of classes to extend memory allocation time, refer to this file Helloworld.css (the blog park does not support TXT format):

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 NGen.exe, you can compile the IL code for the Cost machine code. The above problem can be solved. NGen.exe has two functions:

Speed up application startup. Because the code is compiled into native code, the runtime does not need to spend more time compiling

Reduce the application's assembly. If a single assembly loads multiple processes at the same time. NGen.exe will compile the IL code and save it to a separate file. This allows the code to be shared in a way that is mapped to multiple processes in a "memory-mapped" manner. Avoid a single piece of code per process.

Run the Visual Studio 2008 (2005,2010) command prompt again

Run the following command: NGen install Hello.exe:

Command completion (on my machine about 10 seconds, to be able to re-enter the command to complete) after running Hello.exe will find that immediately can print out Hello world, there is no delay.

For Asp.net,microsoft also provides aspnet_compiler.exe, we can build a script (. bat) Precompiled program in the ASP. The following is an example (from Lao Zhao's program):

%systemroot%\microsoft.net\framework\v2.0.50727\aspnet_compiler-v/P. \mymvcdemo.web.ui-f-errorstack. \Compliled
PAUSE
About Aspnet_compiler.exe Not much introduction, interested in can search their own relevant information.

Do not use the IDE to write C # 's Hello World method

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.