The .NET Framework

The .NET Framework, introduced by Microsoft in 2002, is a programming platform and set of tools for building and running distributed applications in the Internet era. It also contains an object-oriented class library and a collection of reusable types (a type is a representation of data such as classes) that enable you to accomplish many common programming tasks such as file access, string manipulation, and database management. The class library is categorized into modules (referred to as namespaces) and includes types that support a variety of applications such as:

• Console applications
• Windows forms
• ASP.NET applications
• XML web services
• Windows services
• SQL Server applications
• Small device applications

In fact, C# is one of the .NET languages that Microsoft developed. The other languages that use .NET are Visual Basic .NET, J#, and Managed C++, in addition to languages developed by other companies. (See more details on the web site:

All these languages work under the .NET umbrella and use its libraries. Actually, you can write an application with modules written in different languages that support .NET. In addition to the class library, the .NET Framework includes the Common Language Runtime (CLR), an integrated environment for executing applications using the .NET library.

There are other versions of the .NET Framework that work with operating systems other than Microsoft Windows, such as Mono. Mono can run on operating systems such as Mac OS X and Linux. There is also a subset of the .NET Framework called the Microsoft .NET Compact Framework that can be used with small devices and smart phones. There is even a Micro Framework for extremely low-power devices, such as watches.

CLR and Managed Code
The Common Language Runtime is responsible for executing applications and runtime services such as language integration, security enforcement, memory management, and thread execution. The CLR provides metadata, which is a consistent method for describing code. The Common Language Specification (CLS) and the Common Type
System (CTS), fundamental parts of the CLR, define the types and syntax that can be used with many .NET languages. The CTS is a standard that defines how CLR represents and manages types. The CLS is a subset of the features that programming languages must support in order to execute in the context of CLR.

The code that executes under control of CLR, such as C#, is called managed code, while the code that executes without requiring CLR, such as C++, is called unmanaged code. Prior to Visual Studio .NET 2002, all applications used unmanaged code. Applications such as MFC, ATL, and Win32 are unmanaged applications. When you are using managed code, the .NET Framework handles any interaction with the operating system, and you just concentrate on the application itself.

When you compile a C# program, the compilation does not directly generate native (operating system-specific) code; it generates code written in Microsoft Intermediate Language (MSIL or IL). The MSIL code is translated to native code at run time. This compilation phase is called the just-in-time (JIT) compilation.

During compilation, the CLR creates information about your application. It includes class names, field names, method names, and method parameters. This information is called metadata, which means information on data. Metadata is used by the CLR and the Jitter (JIT compiler) for many purposes such as debugging and type checking. You can also use metadata to create instances of the classes and use class members regardless of the source code language.

The program compilation results in creating an assembly, which can be either an .exe file (executable) or a .dll file (library). An assembly contains:

• The manifest that contains the metadata, which provides the following information:
1) Versioning information. The versioning information contains four parts: major version, minor version, build number, and revision number (for example,
2) Security information
3) External assembly references
4) Exported types
5) Culture information (the national language such as English, French, or Chinese)
6) Custom attributes such as company name and product information

• One or more modules of MSIL code

• The resources used by the application

Garbage Collection
One of the most important benefits of using managed applications is the use of the garbage collector (GC). The role of CLR does not end after compiling the IL code into native code. In fact, the CLR is responsible for managing memory during the code execution. It assures that the memory used by the program is totally freed up after the program exits. With unmanaged applications, programmers are responsible for managing the memory and resolving problems that might occur if a block of memory is left allocated after the program ends. With a managed application, blocks of memory are allocated on the managed heap. (The heap is the part of memory that is used to store objects, as opposed to the stack, which is used to store references to objects— more on heap and stack later.) The GC keeps track of the referenced objects on the heap and automatically frees up the memory allocated to a specific object when it goes out of scope. Calling the GC programmatically is possible by invoking the method System.GC.Collect. However, this is not recommended because it is not guaranteed that it will destroy your objects. It is best to focus on the business logic and let the CLR determine the right time for garbage collection.

Putting Things Together
The .NET applications are handled according to the following procedure:
1. You start by coding your application using one of the .NET-compliant languages such as C#.

2. The C# code is compiled into MSIL and stored in an assembly. The
C# code might be split across many source files, which are linked together during the compilation.

3. On executing the code, it is compiled into an operating system specific (native) code by using the JIT compiler.

4. The application is executed under control of the CLR. When the program exits, the GC frees up the allocated memory.

With Visual Studio, you have two programs for IL compilation:

• ilasm.exe: Used to compile programs written in IL and convert them to .exe or .dll files.

• ildasm.exe: Does the opposite process as it reads an .exe or .dll file and retrieves the IL file, which contains the manifest and the metadata. In the next section, we use ildasm.exe to take a look at the contents of the assembly and get an idea about its properties.

Source of Information : Wordware Learn C Sharp Includes the C Sharp 3.0 Features


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner