


In-depth analysis of C# execution principles (pictures and texts)
This article brings you an in-depth analysis (pictures and texts) of C# execution principles. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Why can Unity3D run C#? What is the relationship between C# and Mono? What is the relationship between Mono and .Net Framework? Let’s talk about this topic in depth!
Start with the principle of compilation
Introduce the compiler in one sentence: The compiler converts the source code (source language) written in a certain programming language into another programming language (target language) equivalent form of program. Usually we convert a certain high-level language (such as C, C, C#, Java) into a low-level language (assembly language, machine language).
The compiler works in the form of a pipeline and is divided into several stages: source code → lexical analysis → syntax analysis → semantic analysis → target code → link → executable file. Modern compilers will be more complex, and more processing will be added in the middle, such as preprocessors, intermediate code generation, code optimization, etc.
What is a virtual machine?
Virtual machine (VM), simply understood, can execute specific A program of instructions. In order to execute instructions, some supporting facilities are also needed, such as registers, stacks, etc. A virtual machine can be very complex, so complex that it simulates real computer hardware, or it can be so simple that it can only do addition, subtraction, multiplication and division.
In the field of compilers, virtual machines usually execute a language called intermediate code. The intermediate code is converted from a high-level language to Take Java as an example. What is generated after Java compilation is not an executable file, but a ByteCode (bytecode) file, which contains the code from Java Source code is converted into equivalent code in bytecode form. The Java Virtual Machine (JVM) is responsible for executing this file.
There are two ways for a virtual machine to execute intermediate code: interpretation execution and JIT (just in time compilation). Interpretation and execution means executing each instruction one by one, JIT The intermediate code is first compiled into machine code when it starts running, and then the machine code is executed. Since the intermediate code is executed, different virtual machines implemented on different platforms can execute the same intermediate code, thus achieving cross-platform.
int run(context* ctx, code* c) { for (cmd in c->cmds) { switch (cmd.type) { case ADD: // todo add break; case SUB: // todo subtract break; // ... } } return 0; }
To summarize, the virtual machine itself is not cross-platform, but the language is cross-platform. For developers, they only need to care about the development language, and do not need to care about how the virtual machine is implemented. This is also The reason why Java can be cross-platform is the same as C#. By extension, in theory any language can be cross-platform, as long as supporting facilities such as a compiler or virtual machine are implemented on the corresponding platform.
What is C# and what is IL?
C# is a high-level object-oriented programming language launched by Microsoft based on the .NET framework. Microsoft released this language in 2000, hoping to use this language to replace Java. For more detailed introduction, please refer to the C# Wiki.
C# is a language. Microsoft has customized a language specification for it, providing a complete one-stop service from development, compilation, deployment and execution. The latest specification will be released every once in a while. , adding some new language features. From a grammatical level, C# is a very complete language that is very comfortable to write.
C# Similar to Java, C# will be compiled into an intermediate language (CIL, Common Intermediate Language, also called MSIL). CIL It is also a high-level language, and the virtual machine running CIL is called CLR (Common Language Runtime). Usually we put C#, CIL, CLR, plus a set of basic class libraries provided by Microsoft called .Net Framework.
#C# was born to conquer the universe, but unfortunately, due to Microsoft's closure, this goal has not been achieved. Of course C# is still doing well now, revitalized because of games, because of Unity3D, because of Mono.
.Net Framework vs Mono
Mono is a cross-platform implementation of the .Net Framework. Mono has done a great thing, reimplementing the CLR on all supported platforms and reimplementing the basic class library provided by the .Net Framework.
Above, the work of Compile Time can actually directly use Microsoft’s existing achievements. As long as the CLR of Runtime is implemented on other platforms, this work The quantity is not only large, but also compatibility needs to be ensured. It is a very huge project. Mono has done it. Congratulations!
C
in Unity3DUnity3D embeds a Mono virtual machine. From the above, we can know that when a virtual machine of a certain platform is implemented, the language can run on that platform. Therefore, strictly speaking, Unity3D uses the Mono virtual machine. Run the C# generated IL code compiled by the compiler.
Unity3D uses C# as the development language by default. In addition, it also supports JS and BOO, because Unity3D has developed corresponding compilers to compile JS and BOO into IL.
Summary
C# Under Windows, the IL code is generated through Microsoft's C# compiler and runs in the CLR.
C# On platforms other than Windows, the IL code is generated through the Mono compiler and runs in the Mono virtual machine. You can also directly run the compiled IL code (compiled through any platform ).
Theoretically, if you create a language, implement a compiler for a certain platform, and then implement a virtual machine that conforms to the language specifications for all platforms, your language can run on any platform. .
The above is the detailed content of In-depth analysis of C# execution principles (pictures and texts). 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 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 the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

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

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

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 Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.
