Home > Articles > Programming > Windows Programming

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020