Home > Articles > Programming > Windows Programming

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

Additional Concepts in .NET Framework

This section will cover the some of the namespaces found in the .NET Framework class library. It will also give an introduction to the concept of assemblies in the .NET Framework and explain the terms managed and unmanaged code.

Introduction to Namespaces

Microsoft .NET Framework includes a rich base class library that contains classes, interfaces, and value types that accelerate the development process. This class library provides access to system functionality. To ensure interoperability between languages, the classes in the .NET Framework class library are CLS compliant. Thus, they can be used from any programming language targeted for the Microsoft .NET Framework that complies with the CLS specifications.

Microsoft .NET introduces a concept of namespaces that is similar to packages in Java. Namespaces are logical groupings of functionally related classes. They also help avoid collisions when referring to classes with same name.

The .NET Framework class library contains classes, which perform the following functions:

  • Representing base data types

  • Abstracting common exceptions

  • File I/O handling

  • XML handling

  • Database handling

  • Accessing information about loaded types through reflection

It also contains security related classes, Windows Forms and Web Forms, rich Windows controls, and rich server-side controls for ASP and provides a rich set of interfaces. There are many abstract and concrete classes provided in the class library. User-defined classes can implement any number of the interfaces provided by the .NET Framework class library.

Figure 1-4 shows the Framework class library provided by Microsoft

Figure 1-4Figure 1-4 Microsoft.NET Framework class library.


The System namespace is the root namespace for built-in types provided by the Microsoft .NET Framework. This namespace includes classes that represent the base data types. Any language supported in the .NET Framework can use these base data types. The class Object is the root class in the inheritance hierarchy of the System namespace. Int16, Int32, Byte, Boolean, Single, Double, Char, Array, and String are some examples of members in the System namespace. It is important to note that many of these types correspond to the primitive data types, which are used in programming languages such as Visual Basic, Visual C++, and others.

Functionalities offered by some of the commonly used and important namespaces, which can be used by developer and would be helpful during migration, are as follows:

  • System.Runtime.InteropServices contains classes for interoperability with COM and other unmanaged code. The classes perform all the data marshalling and all other plumbing work required for interoperating with the unmanaged code. This namespace is important during migration because only the main application needs to be migrated to Visual Basic .NET. The COM components can be used as they are through the interoperability mechanism offered by the classes in this namespace.

  • Microsoft.VisualBasic.Compatibility contains classes that are not used in Visual Basic .NET but are preserved so that elements of Visual Basic 6.0 can still be used in Visual Basic .NET during the migration process.

  • DirListBox, DriveListBox, and FileListBox are some of the commonly used controls in Visual Basic 6.0 forms that get upgraded to Visual Basic .NET with the help of this namespace.

  • System.Runtime.Remoting contains classes for creating and configuring distributed applications.

  • System.IO contains classes for basic data stream access and management, including file and memory I/O.

  • System.Reflection contains functionality to access type metadata and dynamic creation and invocation of types.

  • System.Net contains classes for sending and receiving data over a network for commonly used network protocols.

  • System.Threading contains classes for multithreaded programming support, including locking and synchronization.

  • System.Security contains classes for accessing the .NET Framework security system, including policy resolution, stack walks, and permissions.

  • System.Collections contains classes for collections of objects, such as lists, queues, arrays, hash tables, and dictionaries.

  • System.Drawing contains classes for rich two-dimensional graphics functionality and access to GDI+ functionality. Classes provided in this namespace replace graphics functionality offered by individual controls in Visual Basic. More advanced functionality is provided in the System.Drawing.Drawing2D, System.Drawing.Imaging, and System.Drawing.Text namespaces.

  • System.Windows.Forms provides classes for rich user-interface features for Windows-based applications. Classes in this namespace replace the Visual Basic Form object and other control objects.

  • System.Web contains classes for developing Web applications. Classes in this namespace provide core infrastructure for ASP .NET, including Web Forms support.

  • System.Web.Services contains classes for developing SOAP-based Web services and clients for consuming these Web services.

  • System.Data offers a complete set of functionality for all database-related operations. It contains nested namespaces. One namespace, System.Data.SqlClient, provides database-related operations for SQL Server. The other namespace, System.Data.OleDb, deals with other kinds of databases. The System.Data.OleDb namespace provides classes for native data types within SQL Server. These classes provide a safer, faster alternative to other data types.

  • System.XML contains classes for creating and processing XML documents.

Understanding Assemblies in the .NET Framework

An assembly is the unit of deployment in the .NET Framework. It contains code that can be executed in the CLR. Figure 1-5 shows the contents of a typical assembly in the .NET Framework. As seen in the figure, an assembly con tains manifest data and one or more modules. Manifest data contains information about the assembly and other lists of assemblies that it depends on. It also contains all the publicly exposed types and resources. An assembly contains various modules. Each module contains metadata and Intermediate Language (IL). In Microsoft .NET Module can either be an EXE file or a DLL. Manifest in an assembly contains list of dependent assemblies and types and resources exposed by assembly.

Figure 1-5Figure 1-5 Assembly in Microsoft.NET.


Assemblies can be static as well as dynamic. Static assemblies are stored on the disk as portable executable (PE) files. Dynamic assemblies are created at runtime and can be executed directly from the memory. They need not be saved to the hard disk.

The scope of an assembly can be made specific to a particular application by copying the assembly in the application's directory structure. The assembly can also be made global so that all other applications can make use of this assembly. To make the assembly global, it has to be put into the global assembly cache. This is achieved with the help of a global assembly cache tool (gacutil.exe) provided by the .NET Framework.

Microsoft .NET Framework SDK provides a tool called MSIL Disassembler (ILDasm.exe) to view the MSIL. The Intermediate Language Disassembler (ILDasm.exe) allows the developer to load any Microsoft .NET assembly (EXE or DLL) and investigate its contents (including the associated manifest, IL instruction set and type metadata).

The folder AddClass for this chapter contains a small application using Visual Basic .NET.

   Public Class AddClass


      Public Shared Sub Main()
         Console.WriteLine("Addition of {0} and {1} is _ 
            {2}",10, 20, Add(10, 20))
      End Sub

      Private Shared Function Add(ByVal iOperand1 As _
         Integer, ByVal iOperand2 As Integer) As Integer
         Return (iOperand1 + iOperand2)
      End Function
   End Class

In this source code a class named AddClass has been defined. There are two elements in this class: public method Main and private function Add. This code adds two integers: iOperand1 and iOperand2. The source code file named AddClass.vb has been created and compiled in Visual Studio .NET using the Visual Basic .NET compiler. On the command line AddClass.vb can be compiled using vbc AddClass.vb command. This compilation process generates an assembly. To view the contents of an assembly, the ILDasm.exe utility is used as shown in Figure 1-6.

Figure 1-6Figure 1-6 ILDasm utility for AddClass.exe file.


As seen in Figure 1-6, the structure of the assembly is presented in a familiar tree view format. Methods, properties, and so on for a given type are identified by a specific icon. The screen in Figure 1-6 shows that the assembly AddClass.exe contains a default constructor and two static methods named Main and Add. To check the MSIL for the method Add, double-click the Add method. This opens up a separate window displaying the MSIL code as shown in Figure 1-7.

Figure 1-7Figure 1-7 MSIL code for Add method in AddClass.

It is important to note that the same MSIL code is generated irrespective of the language that has been used to develop the application. Thus all Microsoft .NET languages have the same capabilities, and the language used for development depends on the preferences of the developer. In addition, the manifest information for the assembly can be viewed by double-clicking on the Manifest link in the main ILDasm utility. Figure 1-8 shows the information for the AddClass.exe assembly that was created in the previous step.

Figure 1-8Figure 1-8 Manifest Information for AddClass.

As shown in Figure 1-8, the assembly AddClass.exe references the assembly mscorlib.dll, which contains the base class library DLL. Although the entire base class library is broken into several assemblies, the primary binary is mscorlib.dll. This assembly contains a large number of core types, and it provides common programming tasks needed to build .NET applications.

MSIL is a language in its own right. The tool developers guide provided with the Microsoft .NET SDK provides information on the Common Language Infrastructure (CLI). The documentation also provides comprehensive reference documentation for the IL instruction set.

The MSIL code can be stored in file with a .IL extension. This file can be converted into a portable executable file by using a utility called ILASM.EXE. This utility has been provided with Microsoft .NET SDK, and it generates a PE file from MSIL assembly language.

Introduction to Managed and Unmanaged Code

One of the most important concepts in the .NET Framework is managed code, which is code that runs under the supervision of the CLR. The advantage of managed code is that the CLR provides services to managed code such as automatic memory management through a garbage collection mechanism, easy versioning and deployment capabilities, and better security options.

Unmanaged code does not run under the supervision of the CLR. It has to implement memory allocation and deallocation logic. Unmanaged code can be generated in Microsoft .NET by compiling code with the /unsafe option during the compilation process.

During execution of a .NET application, managed code can interact with send and receive data from unmanaged code. This process of passing data between managed and unmanaged code is known as data marshalling. For Microsoft .NET applications that use classic COM components, the classes provided in the System.Runtime.Interop namespace do the data marshalling. The application developer does not have to write code explicitly for the data marshalling in this case.

To call into unmanaged code from managed code, a runtime callable wrapper (RCW) is created. This, however, adds some performance penalty. Similarly, calling into managed code from unmanaged code requires a COM callable wrapper (CCW) to be generated.

  • + Share This
  • 🔖 Save To Your Account