.Net Framework is the most popular platform among Developers because it is more efficient than any other platform or any other programming language. C# is the most popular and widely used programming language in the world. We can build any kind of application with the help of C#. But sometimes programmers become worry about the things under the hood they become worry about how the things work. So here we’ll discuss the things which helps us and makes the things ready for us.
.Net Framework has two Major Components
- · CLR (Common Language Runtime)
- · .Net Base Class Libraries
These are the two major things which plays an important role in the development of any .Net Application.
CLR Big Picture
CLR is actually an execution engine of .Net Framework. It plays its role in .Net Application and Operating System. It makes some important configurations (settings) to deploy the application on Operating System. CLR is an application which is sitting under the hood and it is actually the heart of .Net Framework. It helps in the program to locate, load and manage the .Net objects. Actually it also takes care of program memory management, exception handling, application hosting and for the security checks. It has some major goals
- · IL Code To Native Code
- · Garbage Collection
Here we’ve something detailed picture about .Net Framework and here we have many different blocks available for different purposes but we just need to know the major things and it is not so much beneficial to know all the things. So let’s start our journey with CLR.
CLR is actually an execution engine of .Net Framework. It makes some important configurations (settings) to deploy the application on Operating System. Microsoft CLR understands more than 132 languages.
Actually where we talk about CLR, we also discuss
- · CLS (Common Language Specification)
- · CTS (Common Type Specification)
Let’s suppose we have made a library in C# which we are using in Visual Basic Environment, then how they’ll understand each other. It would be possible all these defined rules which is under the CLR. CLR knows through CLS and CTS what the code has actual meanings.
CLS
As we already know that CLR supports many different programming languages in .Net Framework. And when we write code in any programming language, it has its own syntactical rules which we need to follow. And these rules can vary from different programming language. But CLR understands all these different rules of different programming languages with the help of CLS.
Example
- · In C#, each statement ends with (;) semi-colon.
- · In VB.Net, we should not end up our statement with semi-colon
Both languages is compiled by CLR, and CLR knows how to compile them because it already knows these different rules in these different environments.
CTS
Look each data type has different meanings, different size of container in different programming language. And CTS helps CLR to understand these different styles and make its own CLR defined data type.
Example
- · In C#, we define the datatype
int
Now CTS understands it and make CLR
Int32 (CLR datatype which means under the hood)
- · In Visual Basic, we define the datatype in different style,
Integer
Now CTS also understand this way of datatype declaration and make the CLR datatype
Int32
IL Code To Native Code
As we already know that when the code is compile then there are few parameters of information as well the compilation need like (OS, Hardware, and Different Configurations). So when we compile the code completely from source code to machine code then it is completely ready to deploy. And it would be compatible just only for that machine. Sometimes it works like I compile my code on Windows 7 and try to run on Windows 8 then it would be run but it won’t be run optimally. It would not be run with its full performance.
Similarly if we wanna run our compiled code into any other OS then it becomes crash. Here we need the solution to this kind of Problem.
Microsoft introduces CLR in .Net Framework and here we have a feature of JIT (Just In Time) Compilation. The Purpose of this feature is we just compile our code with its compiler and compiler converts this code into byte code. Now the code has been compiled. But when we wanna deploy it on any targeted machine then JIT makes that byte code to machine code and includes the available information of that machine like (OS, Hardware, and different metadata information or configuration). This is the way JIT Component works.
And now we can run our code on any OS like Mac, Linux, and Ubuntu etc.
If we want to see the IL Code, then we’ve ILSpy which we can download from ‘Extension and Updates’ in Visual Studio.
- · Tools > Extension And Updates
- · And install ILSpy.
Here I’ve already installed it. So Green Tick Mark is showing.
Actually what is IL Code?
IL stands for Intermediate Language. It is actually partially compiled or half compiled code.
Example
- · Make a Console Application and write some code.
static void Main(string[] args)
{
int x = 123;
Console.WriteLine(x);
Console.ReadLine();
}
- · Now build the project.
- · Right click on the project and Select ‘Open Output in ILSpy’
- · Now select ‘IL’ in ILSpy window and click on Program class in ‘Hello World’ ILSpy Project.
And Here You can see how your program IL Code looks like.
Note:
Keep in your mind, if
you want to see the IL Code of your project then you need to build the project
because IL of any program is generated by building the project.
How does JIT compile our code?
There are 3 ways to compile the code.
- · Per File
- · Per Method/Function
- · Code Fragment
But we can’t force JIT about them. JIT will take the decision itself what is the strategy and how does it compile the code.
Garbage Collection
I suppose that you are already familiar with C++ Development Environment. In C++ we make the objects and they remain in the memory until the application finishes its execution. Another example is suppose we are allocating the memory at the runtime then we need to deallocate the memory as well otherwise our memory becomes reserved. Now we can’t use that memory any more whether you stop your program. We actually need to deallocate it at the runtime.
And these kind of things are so much costly for the program. If we measure the complexity of our program then we already know that to measure the complexity there are two major things.
- · Time
- · Space
If our application is playing with many unused objects then it is actually consuming huge space and time as well.
So here Garbage Collector comes into picture. Garbage Collector is the component of CLR which releases the unused managed objects even during the execution of the program. It doesn’t release the (unmanaged) objects which are outside of the CLR like COM objects (Excel, Word etc.) or the objects which are relevant too peripheral operations like (StreamReader, StreamWriter etc.), we need to manually dispose them in our program. And it has few techniques which we will discuss later in the series.
To release the memory at runtime, GC uses the technique of Generations.
Why GC Uses the Technique of Generations?
Because it is so much costly for the GC to analyze each object again and again and check that the object is releasable or not?
So GC uses the technique of Generations.
How Generations Work?
When we create an application then new created objects comes into Gen 0, then GC runs and analyze all the objects that do they need more in the application or not? If they are unused then GC release that objects from the memory and move those remaining objects into the Gen 1.
Still application is keep running and now again new objects being created and now they again analyze into Gen 0 and if they are not important for future they become release and if they are important then they move to the Gen 1. Now GC comes into Gen 1 and analyze the objects if they are important for the future then they move the objects into Gen 2 and if they are not important then GC release that objects from the memory.
And now the process keeps going same as it is.
It is so much difficult to release the objects from Gen 2 and Gen 1 family and it is quite easy to release the objects that are from Gen 0 family.
.Net Base Class Libraries
Now let’s discuss the concept of base class libraries. You might have already seen the header files including in C++ programs.
#include<iostream.h>
#include<conio.h>
These include files are actually its base libraries which supports our built-in functions keywords etc. If we not include these header files we can’t even write a single line.
Similarly, each language has its own header files, packages, libraries. In .Net Environment, we have Base Class Libraries/Framework Libraries.
Base Class Libraries has 2 types
- · User Defined Libraries
- · Pre-Defined Libraries
User Defined Library (Assembly)
In .Net Environment, user defined libraries (means that the program which is written by programmer or developer) are called assemblies. We use these custom assemblies which we build according to our requirement in different programs. Actually the concept of assembly is all about the reusability factor. That we make an assembly and use it in any our program where we want.
It has two types.
- · .exe
.exe (executable program), this is the execution file and it can run independently
- · .dll
.dll (dynamic linked library), it can’t run independently. We add dll file into another program either it is console application, winforms, wpf, web application. And we use this (dll file) here in this application.
Predefined Library (Namespace)
These libraries are actually framework defined, which have built already we just include them and use them.
Like in C# Program, we include the namespace
using System;
This is predefined library.
0 comments:
Post a Comment