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)

Mar 10, 2017 am 11:24 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

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

Access Modifiers in C# Access Modifiers in C# Sep 03, 2024 pm 03:24 PM

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

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

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

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

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.

C# StringReader C# StringReader Sep 03, 2024 pm 03:23 PM

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

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

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.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

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

C# StringWriter C# StringWriter Sep 03, 2024 pm 03:23 PM

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

See all articles