Home > Backend Development > C#.Net Tutorial > Detailed explanation of Hello World in C# that can be written without IDE (picture)

Detailed explanation of Hello World in C# that can be written without IDE (picture)

黄舟
Release: 2017-03-10 11:24:40
Original
1467 people have browsed it

It is very easy to write Hello World in C# using IDE such as Visual Studio, but can you print out Hello World without IDE? This does not mean leaving IDE while working, but learning the execution model of CLR.

Hello World

1. Create a new notepad, enter the following code, and save it as HelloWorld.txt.

using System;
namespace HelloWorld
{
   class Program
   {
        static void Main(string[] args) {
            Console.WriteLine("Hello World!");
            Console.ReadKey();
        }
    }
}
Copy after login

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

Detailed explanation of Hello World in C# that can be written without IDE (picture)

3. Switch to the directory of HelloWorld.txt

Detailed explanation of Hello World in C# that can be written without IDE (picture)

4. Run the command: csc /out:Hello.exe HelloWorld.txt

Detailed explanation of Hello World in C# that can be written without IDE (picture)

If nothing unexpected happens, Hello.exe will be compiled and Hello World can be printed.

CLR execution model - compile time

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

Detailed explanation of Hello World in C# that can be written without IDE (picture)

The compilation period can also be logically divided into two steps:

1. The CLR (C#) compiler accepts source code files and compiles them into managed modules. The managed module includes IL code, metadata, CLR header and other components. In the above example, HelloWorld.txt is compiled into a managed module.

2. Generally, an assembly will contain many source code files (here there is only HelloWorld.txt) and resource files. The second step is to merge the corresponding compilation results of each source code file and resource file into an assembly.

Perform the above two steps to get an XX.dll or XX.exe assembly, just like the Hello.exe above.

How does the compiler know whether to compile into a managed module or a resource file? In fact, the compiler must be explicitly told how to compile each file. This corresponds to the generation operation of Visual Studio's file attributes.

Right-click on any Visual Studio solution file-->Properties-->Generate Operation:

Detailed explanation of Hello World in C# that can be written without IDE (picture)

Specify Class1 as an embedded resource. If you use ILSpy to view it, you will find that Class1 is only embedded in the assembly. The name is: namespace. File name:

Detailed explanation of Hello World in C# that can be written without IDE (picture)

You can even set an Detailed explanation of Hello World in C# that can be written without IDE (picture) to compile and let the compiler try to compile it, but it will report an error.

Runtime

The assembly is generated above. The IL code in the assembly is not yet runnable code. IL is a CPU-independent machine language that is compiled into native code (CPU instructions) by a JIT (Just-in-Time) compiler until the assembly is called. At runtime, the CLR performs the following steps:

1. Check the security features of the assembly;

2. Allocate space in memory;

3. Send the executable code in the assembly to the JIT compiler, and compile part of it into native code (CPU instructions).

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 also discarded.

For example, if you change the above code to:

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

The first WriteLine needs to be JIT compiled first and then executed. Since the WriteLine code has been compiled, the second WriteLine will directly execute the code in the memory block, skipping JIT compilation.

Due to memory allocation, JIT compilation process, etc., the program will cause some performance loss when it is run for the first time. This feeling is particularly obvious when writing ASP.NET. After pressing F5, it will take a long time before the homepage is displayed.

Let’s simulate and experience this process. Use a bunch of classes to extend memory allocation time, refer to this file HelloWorld.cs:

Detailed explanation of Hello World in C# that can be written without IDE (picture)

Run the command again: csc /out:Hello.exe HelloWorld.txt to get Hello.exe. During execution, it is found that there is a certain delay before Hello World is printed.

Generate native code

Using NGen.exe provided by .NET, the IL code can be compiled into native code, which can solve the above problems. NGen.exe has two functions:

1. Speed ​​up the startup speed of applications. Because the code is compiled to native code, there is no need to spend time compiling at runtime.

2. Reduce the application assembly. If an assembly is loaded by multiple processes at the same time, NGen.exe compiles the IL to native code and saves it to a separate file. In this way, it can be mapped to multiple processes at the same time through "memory mapping" to share the code and avoid each process having a copy of the code.

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

Run the following command: ngen install Hello.exe:

Detailed explanation of Hello World in C# that can be written without IDE (picture)  

After the command is completed (it takes about 10 seconds on my machine, it will not be completed until the command can be entered again), run Hello.exe and you will find that Hello World can be printed out immediately without any delay.

The above is the detailed content of Detailed explanation of Hello World in C# that can be written without IDE (picture). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template