Updated March 16, 2023
Introduction to C# Compilers
A C# Complier can be defined as an in-built component that is used for turning the C# programmed code into a machine readable language code, which returns the output files in the form of .dll files or .exe files. C# has two different types of compiling methods, namely Common Language Runtime compiling (CLR) and Just in time compiling (JIT). The JIT compilers are further classified as Pre JIT compiler, Normal JIT compiler and Econo JIT compiler. A few of the commonly used tools for this compilation purposes are C# compiler 2.0, C# compiler 3.5, C# compiler 4.x, Roslyn and Mono Compilers.
So, after the intermediate language generated we required to convert this IL to machine codes because processor only understood machine codes. So we used CLR (Common Language Runtime) for this conversion. CLR is a program that runs on the computer and changes the IL code to machine codes with the help of Just-In-Time (commonly called JIT). When we double-clicked on the .exe file JIT compiler is invoked and compiles the IL code at that part of the time, so that’s why it is named JUST-IN-TIME(JIT).
Compilation can be done implicitly or explicitly. The implicit compilation is twice compilation in which first, it is compiled IS and after that, it is compiled to machine language. This process is called JIT compilation or JUST-In-Time compilation. JIT supports multiple platforms and speeds up the execution of the code.
Types of Just-In-Time Compiler
There are three different Just-In-Time Compiler
1. Pre JIT compiler
In this compiler, there is only a single compilation process as the whole source code is compiled into the native code.
2. Normal JIT compiler
In this compiler, only those methods are compiled into machine code, which is required at run time. The compiler compiles those methods, stores them in the cache, and used the same method again.
3. Econo JIT compiler
In this compiler, only those methods are compiled into machine code which is required at a run time however these methods are removed if they are not required. In JIT, only the required method is compiled at run time, therefore, it requires less memory usage.
In the above diagram
1-3 steps will execute at the compile time
3-5 steps will execute at the run time.
So, when we run these two types of error occurs.
Run-Time Errors and Compiler Errors
- Compiler Errors: This type of error occurs in the C# compiler itself. It prevents the C# code to compile into .exe format. These errors are basically occurred due to syntax error, When we misspelled any syntax or miss the comma or semicolon, then compiler did not understand this so it stops the processing and throws the compile-time error. We can see these errors in the output window of IDE. So by examining these errors we can rectify our code.
- Runtime Errors: These are real-time errors. These occur when code is compiled successfully but encounter any issues while running. This impacts the behavior of the code. For example, we can say when we try to divide any number with zero at that time run time error occurs. These errors are more tedious than compiler errors because in this case, we need to rectify the behavior of the code.
Type of C# Compilers
Earlier Microsoft compilers were named as .Net Framework like C# compiler 2.0, C# compiler 3.5, C# compiler 4.x, etc. These compilers were not written in C# or we can say C# was not compiled by C#. The other compilers which came into existence after this are:
Earlier the compiler was written in C/C++. Roslyn is open source .Net compiler in which the C++ code is replaced. It fetches the types of elements in code, their relation with each other. As C# and VB teams wrote the compiler, therefore, visual studio templates available for both. It allows us to write code which parses a source file and It is easy for us to modify source code.
The following are the four API layers of Roslyn
- Scripting API: This layer provides a runtime execution context and allows us to use C# as a scripting language and has no dependencies on visual studio components.
- Compiler API: This layer is used for syntax and contains references for assemblies and source code files. This layer also has no dependencies on visual studio components.
- Service API: This layer provides IntelliSense, formatting, Find All References, etc. on the top of SDK. It also helps in managing information about the project and manage project dependencies.
- Workspace API: This layer is mainly for analysis of code and refactoring through IDEs. Roslyn makes the code easier and consists of a set of APIs like compiler and service which provides many functionalities like formatting, Intellisense, etc. It provides support to multiple languages. Workspace APIs provide deep information about source code.
2. Mono Compilers
Mono C# compiler based on ECMA standards for C#. It has multiple versions with a framework like C# 1.0, 2.0, 3.0, 4.0. The mono compiler mcs targets framework 1.1, gmcs targets framework 2.0 whereas smcs and dmcs target framework 2.1 and 4.0 respectively.
- mcs: It supports all C# 1.1 features like anonymous methods, nullable types, fixed buffers, access modifiers on properties, static classes, etc.
- gmcs: It supports C# 2.0 features like query expressions, object initializers, partial methods, lambda expressions, automatically implemented properties, etc.
- dmcs: It supports C# 4.0 features which include named arguments, generic type variance, dynamics binding, etc.
The other compiler like RemObjects C# compiler which follows C# standard as per EMCA specification and other than that there are GNU C# compilers that implements C# and .Net.
C# compiler is just like any other compiler which compiles code into machine code so that the processor can understand the process.
This has been a guide to Data Mining Tool. Here we discuss introduction to C# compilers and different types of Just-In-Time Compiler and C# Compilers. You can also go through our other suggested articles to learn more –