


Detailed explanation of Hello World in C# that can be written without IDE (picture)
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(); } } }
2. Open the Visual Studio 2008 (2005, 2010) command prompt program
3. Switch to the directory of HelloWorld.txt
4. Run the command: csc /out:Hello.exe HelloWorld.txt
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:
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:
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:
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(); }
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:
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to C# StringReader. Here we discuss a brief overview on C# StringReader and its working along with different Examples and Code.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to C# StringWriter. Here we discuss a brief overview on C# StringWriter Class and its working along with different Examples and Codes.
