Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Introduction to Microsoft .NET Framework

Microsoft .NET Framework is a new programming model that simplifies application development for the highly distributed environment of the Internet. The major components of the .NET Framework are the CLR and the .NET Framework class library. Figure 1-2 shows the architecture of the Microsoft

Figure 1-2Figure 1-2 Microsoft.NET Framework.

Windows Forms are used in developing rich GUI desktop .NET applications. Web Forms are used to develop Web applications. With the introduction of Web Forms, the .NET Framework brings the ease of drag and drop designing into Web application development.

ADO.NET brings with it the concept of disconnected data access. This vastly improves the performance of ASP.NET applications. ADO.NET components have been logically grouped into data access and data manipulation components. The data access components first access the data. Then a connection with the data source is no longer required. Once data has been retrieved, the data manipulation components manipulate the data according to the business logic. The ADO.NET classes are found in the System.Data namespace. The concept of namespaces is covered in a later section in this chapter.

The CLR, CTS, and the CLS are explained in more detail in the next subsections.

Introduction to the CLR

The CLR is the execution environment provided by the Microsoft .NET Framework. It provides many services such as

  • Automatic garbage collection
  • Code access security
  • Simplified versioning
  • Simple and reliable deployment
  • Deep cross-language interoperability
  • Debugging across different languages
  • Performance
  • Scalability

Because the CLR manages the code execution, all the code that is targeted for the CLR is known as managed code. Managed code emits metadata along with the executable. This metadata is used to describe the types (classes) and members used in the code, along with all the external references used in executing the code. The CLR uses this metadata to load the classes during execution and resolve method invocations during runtime.

The CLR provides automatic garbage collection of the objects that have been loaded into memory. All objects that are created via the new operator are allocated memory on the heap. A program can allocate as many objects as are required by the program logic. However, when an object is no longer required, there must be some mechanism to free up the memory that was occupied by the object.

This is accomplished in the CLR via a program called garbage collector, which collects all objects in memory that have no references. This program runs as a low-priority thread in the background process and collects all unreferenced objects. Because memory management is automatic, the chances for memory leaks in the program are minimized. However, the time when garbage collector would actually release the objects from the memory is not known. This concept is known as nondeterministic garbage collection because it cannot be determined in advance when the objects would be released from memory.

If sufficient memory is not available for creating new objects, the CLR throws an exception that can be caught and gracefully handled by the application.

Code Access Security (CAS), as the name suggests, is used to control the access that the code has to system resources. The CLR has a runtime security system. Administrators can configure policy settings by specifying the resources that can be accessed by the code.

A call stack is created that represents the order in which the assemblies get called. The CLR's security system walks the stack to determine whether the code is authorized to access the system resources or perform certain operations. If any caller in the call stack does not have the requisite permission to access the specific system resources, a security exception is thrown by the CLR.

Simplified versioning is another feature provided in the .NET Framework. It supports versioning and also provides for side-by-side execution of different versions of the same component. The specific versions of the assembly and the dependent assemblies are stored in the assembly's manifest. The copies of the same assembly that differ only in version numbers are considered to be different assemblies by the CLR. Assemblies are explained in more detail in the later sections.

Simplified deployment is one of the features provided in the .NET Framework. The most important point to mention is that .NET components do not need to be registered in the Windows registry. All code generated in the .NET Framework is self-describing because assemblies contain the manifest and metadata information. This information contains all the data about the dependencies of the assembly and the specific versions of the components that these assemblies would use at execution time; therefore, multiple versions of the same components can coexist. The CLR enforces the versioning policy.

Cross-language interoperability is an important feature, and it was one of the design goals of the .NET Framework. This feature is possible because of the CTS and CLS. The CTS is explained in more detail in the next subsection.

Visual Studio .NET allows for debugging across an application consisting of different languages targeted for the CLR. In fact, the IDE also allows for debugging an application in which managed code interacts with unmanaged code.

CLR ensures that performance of the code execution is optimized. Compiled code is stored in cache. When the same code is called next time, this code is loaded into memory from cache. This advantage stands out more in the case of ASP.NET applications than for ASP applications. ASP code was interpreted every time an ASP page was requested. In ASP.NET, the code is compiled only once when the page is requested for the first time. This ensures that performance is optimized.

The .NET Framework also provides some classes for tracking the performance of the .NET applications. These classes are known as performance counters. The .NET Framework provides performance counters for getting information on exception handling, interoperation with unmanaged code, loading and unloading code into memory, locking and threading, memory, networking operations, and so on. These performance counters help to fine-tune the performance of the .NET applications.

Introduction to the CTS

The CTS defines how types are declared, used, and managed in the runtime environment. The CTS is the key element for the CLR's support of cross-language integration. The common type system is used to

  • Enable cross-language integration, type safety, and high-performance code execution.

  • Define rules that languages should follow. This helps to ensure that objects written in different languages can interact with each other.

The Microsoft .NET Framework supports two categories of types, reference and value. As the name suggests, reference types contain a reference to memory address of a value stored in memory. If any changes are made to the value using the reference address, the original value is changed in memory. Reference type variables are allocated in the heap memory.

Value types, on the other hand, contain the actual value. If the value of one variable (of value type) is assigned to another variable (of value type), the contents of the first variable are copied into the second variable. If any changes are made in the second variable, the contents of the first variable are not changed. Value type variables are allocated on the stack. Value types are stored more efficiently as primitive types. Value types are derived from the System.ValueType class. Because of this, the value type variables can have fields, properties, and events, just as reference type of variables.

Microsoft .NET has introduced a concept called boxing and unboxing. Boxing involves the process of converting a value type variable into a reference type variable. Boxing a variable of a value type allocates an object instance on the heap and copies the value of the value type variable into the heap. Unboxing is the explicit conversion from the object type to a value type. During unboxing an InvalidCastException might be thrown if the source argument is null or is a reference to an incompatible type.

A type definition includes the type name, visibility, base type, interfaces implemented by the new type, and members of the new type. A type needs to be identified by a name. It can have global access; that is, all other assemblies can access the type if the accessibility of the type is public. If the accessibility of the type is assembly, the type can be accessed only within the assembly in which the type is defined. A type can inherit from other types and extend the behavior of the base types. A type can inherit only from a single type. It can also implement any number of interfaces. In addition, attributes can be used with the types to provide more information about the types.

Value types are built-in data types provided by the programming languages supported in the Microsoft .NET Framework. Integer, Float, and Double are some examples of built-in data types and are value types. User-defined value types can be defined. Structure is a common example of a user-defined value type that is supported in Visual Basic .NET.

Some of the reference types found in the Microsoft .NET Framework are the classes, arrays, pointers, delegates, and so on.

Introduction to the CLS

The CLS rules define the basic language features required by many applications and promoting language interoperability. The CLS defines a subset of the CTS (explained in the previous section). All languages targeted for the Microsoft .NET Framework comply with the CLS. This ensures language interoperability between the various languages. The CLS also establishes requirements for CLS compliance; these help developers determine whether their managed code conforms to the CLS.

The CLS rules are normally used from the perspective of the high-level source code and tools, such as compilers, that are used in the process of generating assemblies in the Microsoft .NET Framework.

The CLS rules apply only to externally visible items. Within a single assembly there are no restrictions to the programming techniques that are used. Code can be marked as CLS compliant or otherwise with the help of custom attributes.

An assembly can contain many types. If an assembly is marked as CLS compliant with a custom attribute, all types within that assembly are automatically CLS compliant. However, one can also mark individual types with custom attributes to make them non-CLS compliant. Similarly, if a type is marked as CLS compliant, all the members of that type are automatically CLS compliant unless marked as non-CLS compliant with the help of attributes.

Most of the classes found in the .NET Framework class library are CLS compliant. Thus, these classes can be used from all languages that are targeted for the .NET Framework.

Understanding Compilation in .NET Framework

Compilation is a two-step process in the .NET Framework, as shown in Figure 1-3. In the first step, the language compiler generates Microsoft

Figure 1-3Figure 1-3 Compilation in Microsoft.NET.

In the second step of compilation, the just-in-time (JIT) compiler compiles MSIL into native code, which can be executed on specific hardware and operating systems. The JIT compiler is a part of the runtime execution environment.

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.

The security policy settings are referred at the compilation stage. If the code is not type-safe, the JIT process is aborted and an exception is raised. This type safety is ensured during compilation using JIT.

Overall the role of a JIT compiler is to bring higher performance by placing the compiled code in cache so that when the next call is made to the same method or procedure, it gets executed at a faster speed.

  • + Share This
  • 🔖 Save To Your Account