Home > Articles > Programming > Windows Programming

This chapter is from the book

This chapter is from the book

5.2 The Common Language Runtime

The runtime environment for the .NET Framework is called the Common Language Runtime (CLR). Managed code execution happens inside the CLR space. The goal of the CLR is to provide an environment that includes language integration, exception handling, security, versioning, deployment, debugging, profiling, and component interaction. Most importantly, all of these features need cross-language support. In other words, all the features mentioned must work in the same manner regardless of the language used.

Metadata makes cross-language integration possible. When you compile .NET managed code, the metadata gets stored along with the object code. Metadata describes to the CLR various types of information (for example, data types, members, and references) used in the code. This data is used by the CLR to "manage" the code execution by providing such services as memory allocation, method invocation, and security enforcement. It also eases deployment since references to other objects are included along with metadata. This ensures that your application contains all up-to-date versions of all dependent components.

5.2.1 The Common Type System

Along with providing information about managed code through metadata, the CLR implements a series of data types that are cross-language compatible. That system of data types is known as the Common Type System (CTS). CTS data types include simple value types, classes, enumerated value types, interfaces, and delegates.

Simple value data types include primitive types as well as user-defined types. Primitive types include integers, Boolean values, and strings. These types are included in the System namespace. The data types used thus far in the VB.NET programs in this book are also included in this namespace. When you use these primitive types in your programs, the language you use may already have an equivalent native data type that corresponds to a .NET Framework type. Table 5-1 shows some data types and their VB.NET native language equivalents.

Table 5-1 Primitive Data Types and VB.NET Equivalents

NET Data Type Class Name (System Namespace)

VB.NET Data Type




Not supported








Not supported


Not supported


Not supported















Occasionally you may want to define your own data types. You can do this by using the native features of the language with which you're working. If you're using VB.NET, you can use the Structure statement to define a structure. This custom type needs to be type-safe for the CLR, so it's no coincidence that it inherits from the ValueType .NET class.

techTalk: structures and classes

Structures and classes are quite similar. They both have members, constructors, events, properties, fields, and constants. They can both implement interfaces as well.

There are some differences. Structures don't allow for inheritance; therefore they are referred to as sealed.

Structures can't have any constructor code, that is, you can't define an overloaded New() subroutine with your own initialization code like you can with a class.

When structures are used in procedure calls, the structure is passed by value (like primitive data types are). Classes, on the other hand, are always passed by reference. Whether to use classes or structures for your user-defined data types depends mostly on the complexity of your data types. Structures are useful for defining relatively simple data types for which the members do not use much memory and no custom initialization code is required. If your design requires more than this, consider defining your custom data type as a class.

Let's quickly look at a VB.NET structure definition. Structure definitions are placed outside of procedure definitions. You can define them at the module level, as shown below.

Code Sample 5-1 Build instructions: vbc /r:system.dll cs5-01.vb

Module Module1

 Structure Student
     Dim FirstName As String
     Dim LastName As String
     Dim SSN As String
     Dim ClassRank As Integer
   End Structure

   Sub Main()

     Dim udtStudent As New Student()

   With udtStudent
       .FirstName = "Matt"
       .LastName = "Crouch"
       .SSN = "888-88-1234"
       .ClassRank = 2
     End With

   End Sub

End Module

The example shows a typical structure definition. The members are enclosed in the Structure...End Structure block that begins in line . The VB.NET With...End With statement in line is used to save some typing. It allows you to refer to the individual members of the structure without fully qualifying the names of the structure members.

Since the VB.NET structures you define automatically inherit from System.ValueType, you can treat the value type as a ValueType object. As a demonstration, let's create a function that lists all the members of an arbitrary structure at runtime.

Code Sample 5-2 Build instructions: vbc /r:system.dll cs5-02.vb

Public Sub ValueTypeDemoFunction(ByVal udt As ValueType)

   Dim mi() As MemberInfo
   Dim srmMemberInfo As MemberInfo

   Dim typTmp As Type
 typTmp = udt.GetType(udt.ToString())
 mi = typTmp.GetMembers()

   Console.WriteLine("Value Type Information" & _
                     Chr(13) & Chr(10))

   For Each srmMemberInfo In mi

End Sub

The function ValueTypeDemoFunction() takes a ValueType object as its parameter. Thus we can pass a VB.NET structure to this function. The GetType() function in line , which is a member of the System.Object namespace, returns a System.Type object. We use the returned System.Type object in line to get the member names (an array of System.Reflection.MemberInfo objects).

If we modify Code Sample 5-1 to add a call to ValueTypeDemoFunction() as shown below in boldface text, we'll obtain output similar to Figure 5-1.

Figure 5-1Figure 5-1 Output of the ValueType example

Sub Main()

   Dim udtStudent As New Student()

   With udtStudent
     .FirstName = "Matt"
     .LastName = "Crouch"
     .SSN = "888-88-1234"
     .ClassRank = 2
   End With


End Sub

5.2.2 Just-in-Time Code Compilation

Managed code cannot be executed directly by the CPU. It must be converted to native executable code before running. Just-in-time (JIT) compilation compiles MSIL code right at the moment it is needed. Optimizations exist in the JIT compiler to ensure that only code planned for execution gets compiled. The JIT compiler also performs security checks and verifies type-safety.

5.2.3 Code Assemblies

I've mentioned the topic of component-based system architectures before, and now it's time to introduce the .NET Framework concept of this idea. The component (that is, the unit of reuse) in the .NET Framework is the assembly. An assembly is a collection of files, typically .dll files and any others relating to the assembly, such as resource files. The assembly manifest contains metadata relating to version information, security attributes, and external code references. It also contains information on how the pieces in the assembly relate to each other. The assembly manifest, therefore, constructs a logical DLL around the assembly elements.

5.2.4 Application Domains

Modern software systems run applications that are isolated from the internal execution of the operating system and other programs. The reason for this is to protect the operating system from crashing if the application attempts to access memory being used by another application. Another situation that could cause a crash is an internal error in the application that causes the operating system to crash. Fortunately, all versions of Windows offer process protection to prevent this problem. Each application running under Windows has its own memory space, and memory used by other running applications is not visible from any other application.

The .NET Framework extends the capabilities of protected process spaces by building this functionality into the CLR. These protected spaces are known as application domains. In addition to the fault tolerance that process isolation provides, application domains can enforce security policies, thereby granting or denying users and groups the right to run the application. Application domains also consume fewer system resources than traditional Windows processes because they can provide fault tolerance by taking advantage of the inherit type-safety of the .NET Framework code.

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.


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.


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.


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.


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


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


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.


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.


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