.NET Framework At A Glance

The Philosophy of .NET
 
C# programming language and the .NET platform were introduced by Microsoft in 2002 and have quickly become a need for modern-day software development.
 
Initial look at the .NET Platform
 
Before .NET, Windows programming using Microsoft platform was divided into number of branches. At that time, C, C++, and Visual Basic were used. Some C and C++ programmers were using raw win32 API and most of the programmers were using MFC (Microsoft Foundation Classes), while the others had moved to COM.
 
Despite the complexity and limitations of COM, countless applications have been successfully created with this architecture. But nowadays, desktop applications, web applications, OS Services, and libraries of reusable data access/business logic are created using .NET.
 
What is the .NET Framework?
 
“.NET framework is a software platform designed to build systems on Windows, Mac OS, and Linux/Unix distributions.”
 
“NET framework is the managed code and platform-independent programing model. It provides a highly productive environment for software developers and offers excellent skills reuse across multiple application architectures.”
 
Some Key Benefits of the .NET Platform
 
.NET framework was designed to address the problems and limitations faced in old programming techniques. Some of the key benefits of the .NET framework are mentioned below -
  • Multiple Platforms
     
    It runs on a broad range of computers from Servers and desktop machines to PDA’s and cell phones.
     
  • Security
     
    It provides much safer execution environment even in the presence of the code obtained from the suspect source.
     
  • Interoperability with existing code
     
    A program written in one .NET language can be used and even inherited from the class written in another .NET language.
     
  • Platform Invoke
     
    .NET Framework provides a feature called platform invoke (P/Invoke) which allows you to use code written for .NET in code not written in .NET.
     
  • Interoperability with COM
     
    .NET framework provides interoperability with COM. COM components can call .NET components and .NET components can call COM components.
     
  • Support for numerous languages
     
    .NET application can be written in any number of programming languages for example (C#, Python, F#, VB.NET etc.).
     
  • Communication using Industry Standards
     
    The system uses industry-standard communication protocol such as HTTP, SOAP, XML, WSDL, and JSON.
     
  • Object-Oriented Development Environment
     
    The CLR, BCL, and C# are designed to be object-oriented and act as a well-integrated environment.
     
  • Simplified Development
     
    .NET libraries are not registered into the registry. In addition, .NET allows multiple versions of the same .dll to be exist on a machine.
Introducing CLR, CTS, and CLS
 
From a programmer’s point of view, .NET can be understood as a runtime environment and a comprehensive base class library. The runtime environment is known as Common Language Runtime (CLR). The primary role of CLR is to manage, load, and locate .NET objects on your behalf. CLR also takes care of memory management, application hosting, thread management, and perform basic security features.
 
Another block of .NET framework is Common Type System (CTS). The CTS describes all possible data types and all programming constructs supported by runtime. (Scroll down for more information on CTS).
 
A .NET-aware language might not support every features defined by the CTS. The Common Language Specification CLS is a specification that defines a subset of common types and programming constructs that all .NET programming languages can agree on. So, if you build a .NET type that follows only CLS features, then you can assure that every .NET aware language can use them. Or, if you are using data type or programming construct that is outside of the specifications of the CLS, you cannot guarantee that every .NET programming language can interact with your code.
 
Role of Framework Class Library (FCL)
 
.NET framework provides an extensive library called Base Class Library which is sometimes also called Framework Class Library (FCL). You can use these libraries when writing your programs some of them are XML classes, collection classes, threading and synchronization classes etc. Not only does BCL provide collection of classes, but it also provides support for services required by applications.
 
You can use these libraries to build any type of software application. For example, you can use ASP.NET to build web-based applications, REST services, WCF to build distributed systems, WPF to build desktop applications etc.
 
You can use these libraries to build any type of software application. For example, you can use ASP.NET to build web-based applications, REST services, WCF to build distributed systems, WPF to build desktop applications etc.
 
 .NET Framework
 
The CLR, CTS, CLS, and base class library relationship.
 
 .NET Framework
 
What Makes C# a Popular Programming Language?
 
C# programming language is written in C and C++ whose syntax is similar to the syntax of JAVA. However, calling C# a copy or duplicate of java is inaccurate. C# and java, both belong to the C family. Here is the list of core C# features that are found in all versions of the language.
  • No pointers required.
  • Automatic memory management through garbage collection.
  • Constructs according to the syntax for classes, delegates, interfaces, structs, and enumerations.
  • Ability to overload operators like C++, without complexity.
  • Support for attribute-based programing.
With the release of .NET 2.0 in 2005, more functionalities were added in C#.
  • The ability to build generic types and generic members.
  • Support for anonymous methods.
  • Ability to define single type across multiple code files using the partial
.NET 3.5 (released in 2008) added even more functionality to C# programming language.
  • Support for strongly types queries (LINQ).
  • Support for extension methods.
  • Addition of Lambda expression, which simplifies working with delegates.
.NET 4.0 (released in 2010) updated C# with more features.
  • Support for optional method parameters as well as named method arguments.
  • Support of Dynamic
  • COM interop.
.NET 4.5 added more functionality to C# programming language.
  • Inline initialization for automatic properties as well as support for read-only automatic properties.
  • Support for static which provides direct access to the static members within a namespace.
  • A null conditional operator helps check for null parameters in a method.
  • Support for string interpolation.
  • Ability to filter exceptions using the when
Managed vs. Unmanaged Code
 
C# can only be used to build software that runs under the .NET framework. You can never use C# to build C or C++ style applications. The code that runs under the Common Language Runtime (CLR) is managed code, or the code targeting the .NET runtime is managed code. And the code is not hosted under the .NET framework or not targeted to the .NET framework is unmanaged code. Managed code is stored in the binary unit called assembly. This environment makes it possible to build .NET programs on wide variety of machines.
 
 .NET Framework
 
Image Link -
 https://i-msdn.sec.s-msft.com/dynimg/IC104620.jpeg
 
.NET Assemblies
 
.NET binaries contain IL (Intermediate Language) and type metadata. IL is also known as MSIL (Microsoft Intermediate Language) or alternatively as the CIL (Common Intermediate Language). They do not contain platform-specific information. Remember that .NET libraries have same file extension as unmanaged windows libraries (.dll and .exe).
 
 .NET Framework
 
The compiler for a .NET language takes a source code file and produces an output file called an assembly. The code in an assembly is not native machine code but CIL. When a .dll or .exe file has been created using any .NET language compiler, that binary file is known as assembly. As assembly contains CIL code, this is conceptually simple as Java bytecode. In that it is not compiled to platform-specific instruction until necessary. In addition, CIL also contains metadata that describes characteristics of every type within the binary.
 
CIL, type metadata, and assemblies themselves are also described using metadata which is termed as manifest. The manifest contains information about the current version of the assembly, the culture information, and all externally referenced assemblies that are required for execution.
 
Compiling to Native Code and Execution
 
The program is not complied to native machine code until it is called to run. CLR manages program’s execution at runtime and performs the following steps:
  • It checks the security characteristics of the assembly.
  • It allocates space in memory.
  • It sends the CIL to just-in-time (JIT) compiler which compiles it to native code.
The JIT compiler the CIL code in the assembly and only when needed and it’s then cached. The code that is not called during execution is not compiled to native code. And the code that is called need only to compile once.
 
 .NET Framework
 
Compilation occurs at runtime.
 
Once CIL is compiled to native code, CLR manages it at runtime and perform tasks such as checking parameters, checking array bounds, and exception handling etc.
 
Figures Shows Compile-Time and Runtime Process
 
 .NET Framework
 
The Role of the Assembly Manifest
 
As I described above, the assembly also contains metadata that describes the assembly itself which is termed as manifest. Among other details all external assemblies are required by the current assembly to perform correctly, the assembly version number, copyright information etc. Compiler is responsible to generate assembly’s manifest. Here is an example of the manifest generated by the compiler when compiling the Example.cs. 
  1. .assembly extern mscorlib   
  2. {    
  3. .   publickeytoken = (A1 2B 3C 45 67 89 D0 1E)    
  4.    .ver 4:0:0:0   
  5. }   
  6. .assembly Example  
  7. {   
  8.    .hash algorithm 0x00008004    
  9.    .ver 0:0:0:0   
  10. }   
  11. .module Example.exe   
  12. .imagebase 0x00400000   
  13. .subsystem 0x00000003   
  14. .file alignment 0x00000200   
  15. .corflags 0x00000001   
The Common Type System (CTS)
 
The Common Type System (CTS) defines the characteristics and types that must be used in the managed code. CTS defines how types are declared, used, and managed in the Common Language Runtime (CLR). The CTS performs the following functions:
  • Establish a framework that helps enable cross-language integration, type safety, and high-performance code execution.
  • Provides an object-oriented model that supports the complete implementation of .NET aware programming languages.
  • Defines rules that all .NET aware languages must follow, which helps to ensures that objects written in different languages can interact with each other.
  • Provides a library that contains the primitive data types such as BooleanByteCharInt32, and UInt64.
CTS is a formal specification that documents how types must be defined in order to be hosted by the CLR. It is important to learn how to work with five types defined by the CTS. One of the most important characteristic of CTS is that all types are derived from a common base class called System.Object.
 
Types in the .NET Framework
 
The common type system in the .NET Framework supports the following five categories of types:
  • Class
  • Struct
  • Interface
  • Delegate
  • Enum
Data Types in the .NET Framework
 
 .NET Framework
 
The Common Language Specification (CLS)
 
The CLS specifies the rules, properties, and behaviors of a .NET compliant programming language. The topics include data types, class construction, and parameter passing.
 
The Common Language Runtime
 
CLR can be understood as a collection of services that are required to execute a given compiled of code. CLR is a core component of the .NET framework which sits on the top of the operating system and manages program’s execution. CLR also performs the following services,
  • Garbage collection
  • Memory management
  • Managing exceptions
  • Security and authentications
  • Thread management
     
     .NET Framework
Resources for this post
  • https://msdn.microsoft.com/en-us/library/zw4w595w(v=vs.110).aspx
  • https://msdn.microsoft.com/en-us/library/ms973872.aspx
  • https://msdn.microsoft.com/en-us/library/8bs2ecf4(v=vs.110).aspx
  • https://msdn.microsoft.com/en-us/library/zcx1eb1e(v=vs.110).aspx