.NET Framework Basics


The .NET Framework is a technology that supports building and running the next generation of applications and XML Web services. For more details go to MSDN

The .NET Framework is a runtime execution environment that manages applications that target the .NET Framework. It consists of the common language runtime, which provides memory management and other system services, and an extensive class library, which enables programmers to take advantage of robust, reliable code for all major areas of application development.


Basic Terms:-

MSIL/CIL/IL: - Intermediate Language is also known as MSIL (Microsoft Intermediate Language) or CIL (Common Intermediate Language). All .NET source code is compiled to IL. IL is then converted to machine code at the point where the software is installed.

 
JIT: - Just-In-Time compiler, coverts IL to Machine Code (0/1 Format). When it is required within code execution otherwise it will not do nothing with that MSIL code.
In Microsoft .NET there are three types of JIT compilers:
• Pre-JIT: - Pre-JIT compiles complete source code into native code in a single compilation cycle. This is done at the time of deployment of the application.
• Econo-JIT: - Econo-JIT compiles only those methods that are called at runtime. However, these compiled methods are removed when they are not required.
• Normal-JIT: - Normal-JIT compiles only those methods that are called at runtime. These methods are compiled the first time they are called, and then they are stored in cache. When the same methods are called again, the compiled code from cache is used for execution.

 
CLR: -CLR (Common Language Runtime) is heart of .Net Framework, Its runtime which will take care of program/code execution. Following are the responsibilities of CLR:- 

 
1. Garbage Collection: - Release memory occupied by object which are no longer referred. CLR automatically manages memory thus eliminating memory leaks. When objects are not referred, GC automatically releases those memories thus providing efficient memory management.

 
2. Code Access Security: - CAS grants rights to program depending on the security configuration of the machine. Example the program has rights to edit or create a new file but the security configuration of machine does not allow the program to delete a file. CAS will take care that the code runs under the environment of machines security configuration.

 
3. Code Verification: - This ensures proper code execution and type safety while the code runs. It prevents the source code to perform illegal operation such as accessing invalid memory locations etc.

 
4. IL (Intermediate language)-to-native translators and optimizer's:- CLR uses JIT, compiles the IL code to machine code, and then executes. CLR also determines depending on platform what is optimized way of running the IL code.

 
CTS (Common Type System): - In order that two language communicate smoothly CLR has CTS. So for example "Integer" data type in VB6 and "int" data type in C++ will convert it to System.int32, which is data type of CTS. 

 
CLS (Common Language Specification): - This is a subset of the CTS, which all .NET languages are expected to support. Microsoft defined CLS, which are nothing but guidelines that language should follow in order to communicate with other .NET languages in a seamless manner.

 
Managed code: - Managed code runs inside the environment of CLR i.e. .NET runtime. In short, all IL are managed code. However, if you are using some third party software example VB6 or VC++ component they are unmanaged code, as .NET runtime (CLR) does not have control over the source code execution of these languages.

 
Assembly: - Assembly is unit of deployment like EXE or a DLL. 
  • An assembly consists of one or more files (dlls, exe's, html files etc.) 
  • Manifest contain metadata (data of data). The manifest is part of the assembly, thus making the assembly self-describing.
  • Manifest contains: - Version of assembly, Security identity, Scope of the assembly, Resolve references to resources and classes.
  • Multiple versions can be deployed side by side in different folders. These different versions can execute at the same time without interfering with each other. 
  • Assemblies can be private or shared. 
  • For private assembly deployment, the assembly is copied to the same directory as the client program that references it. No registration is needed, 
  • In shared assembly deployment, an assembly is installed in the Global Assembly Cache (or GAC). The GAC contains shared assemblies that are globally accessible to all .NET applications on the machine.
There are two types of assembly Private and Public assembly. 
  1. "A private assembly is normally used by a single application, and is stored in the application's directory, or a sub-directory."
  2. "A shared assembly is normally stored in the global assembly cache, which is a repository of assemblies maintained by the .NET runtime."

     
Namespace: - The namespace keyword is used to declare a scope that contains a set of related objects. Basically namespace logically group types, for example System.Web.UI logically groups UI related features. 

  • Difference between Namespace and Assembly:-Assembly is physical grouping of logical units, Namespace, logically group classes. Namespace can span multiple assembly.

     
  • GAC:-GAC (Global Assembly Cache) is where all shared .NET assembly reside. GAC is used in the following situations:-
    • If the application has to be shared among several application.
    • If the assembly has some special security, requirements like only administrators can remove the assembly. If the assembly is private then a simple delete of assembly the assembly file will remove the assembly.
    • Using MS installer OR GACUTI from VS command prompt we can install an assembly in GAC.
  • DLL Hell:- It's when Application A installs a Shared DLL version 1.0, Application B comes and updates the Shared DLL to version 1.1 which should be compatible but there are slightly different behaviors, then App A stops working correctly and reinstalls version 1.0 then App B stops working. This problem of dynamic link library (.dll) is resolved through Versioning, in other words by strong naming assembly.

     
  • Strong name is similar to GUID (It is supposed to be unique in space and time) in COM components. Strong Name is only needed when we need to deploy assembly in GAC. Strong names helps GAC to differentiate between two versions. Strong naming use public key cryptography (PKC) to ensure that no one can spoof it. PKC use public key and private key concept.
    One can generate SNK (Key file) by using VS command prompt> SN.EXE> after giving file path, file is generated in defined folder. Then in project properties> signing> USE key file, we can refer generated key file.

     
Reflection: - All .NET assemblies have metadata information stored about the types defined in modules. This metadata information can be accessed by mechanism called as "Reflection".

 
Value types & Reference types: - Value types directly contain their data that are either allocated on the stack or allocated in-line in a structure. Value types are actual data. Reference types store a reference to the value's memory address, and are allocated on the heap. Reference types can be self-describing types, pointer types, or interface types. You can view reference type as pointers to actual data.

  • Boxing and Unboxing: - When we convert value type to a reference type it's termed as boxing. And vice-versa is Unboxing
    • Implicit conversion of a value type (int, char etc.) to a reference type (object), is known as Boxing. 
    • In boxing process, a value type is being allocated on the heap rather than the stack. 
    • Explicit conversion of same reference type (which is being created by boxing process); back to a value type is known as unboxing. 
    • In unboxing process, boxed value type is unboxed from the heap and assigned to a value type which is being allocated on the stack.


Comments