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 the Microsoft platform was divided into a 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 them. NET.

What is the .NET Framework?

Some Key Benefits of the .NET Platform

.NET framework was designed to address the problems and limitations faced by old programming techniques. Some of the key benefits of the .NET framework are mentioned below.

Introducing CLR, CTS, and CLS

  1. 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, and thread management, and performs basic security features.
  2. Another block of .NET framework is the 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).
  3. 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)

  1. .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.
  2. 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.
  3. 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.

Library relationship

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.

With the release of .NET 2.0 in 2005, more functionalities were added in C#.

.NET 3.5 (released in 2008) added even more functionality to the C# programming language.

.NET 4.0 (released in 2010) updated C# with more features.

.NET 4.5 added more functionality to the C# programming language.

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. 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 a wide variety of machines.

Unmanaged Code
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 CIL (Common Intermediate Language). They do not contain platform-specific information. Remember that .NET libraries have the same file extensions as unmanaged Windows libraries (.dll and .exe).

.NET Assemblies

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 does not comply with native machine code until it is called to run. CLR manages the program’s execution at runtime and performs the following steps.

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 into native code. The code that is called needs only to be compiled 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 Processes.

Compile-Time

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. The compiler is responsible for generating the assembly’s manifest. Here is an example of the manifest generated by the compiler when compiling the Example. cs.

.assembly extern mscorlib
{
    .publickeytoken = (A1 2B 3C 45 67 89 D0 1E)
    .ver 4:0:0:0
}
.assembly Example
{
    .hash algorithm 0x00008004
    .ver 0:0:0:0
}
.module Example.exe
.imagebase 0x00400000
.subsystem 0x00000003
.file alignment 0x00000200
.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.

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 characteristics 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.

Data Types in the .NET Framework

Data type

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 compilation of code. CLR is a core component of the .NET framework which sits on the top of the operating system and manages the program’s execution. CLR also performs the following services,

Resources for this post.