What is IL code, CLR, CTS, CLS, JIT ?
How Exactly Dot Net application compiles and Run?
- We write our code in C# or VB.net
- Then we compile our program then the compiler will convert our C# or VB.Net program into half compile code i.e IL (Intermediate Language ).
- When you click on Exe .net execution engine start then CLR gets this half compile code and gives to JIT
- A machine can’t understand Intermediate code so JIT goes line by line and convert half compile code into machine-specific instruction.
- Finally, your application executed.
IL Code- Intermediate Language
This code used in .net framework to generate machine-independent code as an output of the compilation.
It is CPU independent half compile code.
Why we need half compile code and why not in one go it convert to machine-specific instruction?
When we creation application we don’t know which windows version is installed in client machine like window XP, windows vista, window 9,10 like that. Also at the time of development, we don’t know the CPU configuration and other machine details.
CLR (Command Language Runtime)
It is the heart of the .Net framework after the compilation process CLR plays the main role in running your application
- Garbage collection
- Class Load
- CAS code access and code verification
- Gives IL code to JIT
CTS (Common Type System)
In .net framework umbrella, there are so many languages present and there might be condition code in one language you need to access in another language so there is a need for smooth communication between these languages we need a common type system.
The common type system ensures that the data type defined in 2 different languages gets compiled into one common type.
Suppose you declared an int variable in c# and integer in VB.net then when it compiles into IL code both variables are compiled into int32. This is CTS.
CLS (Common Language Specification)
It is a subset of CTS. It is a specification and whatever language you used should follow the common specification. It is guidelines that specify if it follows guidelines given by .net framework then it will easily be consumed by .net support language.
How you define your code is CLS compliance?
First, go to assembly.info class and add a tag [assembly: CLSCompliant(true)] it means CLS compliant is enabled for this assembly. This is used when you write code and compiles that code then if it doesn’t follow the compliance then it will throw a warning.
JIT (Just In Time)
JIT translates the MSIL code of an assembly into native code and for that, it uses the CPU architecture of the target machine. There are different types of JIT
- Pre JIT
- Econo JIT
- Normal JIT
In a single run it will translate all source code into native code. It is done at the time of deployment and in .net it is called “Ngen.exe”.
The above command is will convert your application into machine language but conversion is as per machine specific.
Ngen stands for Native Image Generator.
It will compile only those methods which are called at run time and after execution, it will remove from memory. It mainly used in mobile applications.
It compiles only those methods which are called at run time. When the first-time compilation is done it is saved in cache memory. If you again call this method it will not compile this method rather than it will take from cache memory. When you run your application from windows normally it uses Normal JIT. To check this add .NET CLR JIT counters in performance counter and check value fluctuating.
GitHub Projects Link : – https://github.com/SagarJaybhay/DotNetDemos