Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
168
Добавлен:
24.05.2014
Размер:
25.78 Mб
Скачать

xxxIntroduction

Notes. Notes give additional information that may be of interest to the reader but is not essential to performing the task at hand.

Cautions. Cautions are used to warn users of possible disastrous results if they perform a task incorrectly.

New term definitions. All new terms have been italicized and then defined as a part of the text.

 

 

Y

 

L

 

F

 

M

 

A

 

E

 

T

 

 

Team-Fly®

PART I

Introducing VC++.NET

This page intentionally left blank

Chapter 1

Basics of .NET

Framework

“.NET” is the buzzword in the IT industry today. What is .NET? What led to the advent of this? What is new in it and how different is it from the already existing technologies? Above all, what is in it for you and me, the customers? These are some of the questions that I will be addressing in this chapter.

The IT industry is undoubtedly one of the ever-evolving industries. Changes resulting in some revolution have been the rule of life in this industry. Just recall the days when stand-alone PCs came in like a breath of fresh air, followed by networking. To begin with, we were working with intranets, and then slowly moved to the Internet. The Web is truly the biggest revolution to date, because the whole world is now tuned to the Internet. However, every change brings along some new challenges. True to these words, the advent and the major success of the Internet led to a shift in paradigm, from desktop computing to distributed computing. Because the Web is heterogeneous, the biggest challenge for programmers is to create applications with features such as platform independence and interoperability.

As usual, Microsoft realized the shift in paradigm and capitalized by launching the .NET initiative. The .NET initiative aims to enable applications to interact with each other through the Internet by bridging the differences, if any, in the development platforms, the geography (the location), and the languages used by the applications. For instance, an application developed for the Windows platform can now interact with an application developed for the Linux platform. Similarly, an application developed using Visual C++ can now interact with another application developed using Visual Basic. In short, this is what .NET is all about!

Before shifting the focus to VC++.NET, the salient features of the .NET initiative will be discussed: the .NET Framework, its components, and the Visual Studio .NET studio with the various languages that it supports. Then, the discussion will move to the new features and enhancements in Visual C++ .NET, including a comparison of Visual C++ .NET with VC++ 6.0 to highlight the differences between the two.

BASICS OF .NET FRAMEWORK

Chapter 1

5

 

 

 

The .NET Framework

One of the prime aims of the .NET initiative is to provide a common framework that can help you to develop with ease Web-based applications and other services apart from the traditional applications, such as Windows-based applications. To facilitate this, the .NET Framework was designed. The .NET Framework acts as a common framework for multiple languages, such as Visual Basic .NET, Visual C++ .NET, and Visual C# .NET (where C# is pronounced as C sharp), and thus provides a new level of interoperability. Besides this, it provides a set of new and advanced features, such as garbage collection, that enable you to create highly secure applications. The two main components of the .NET Framework that make possible all the features previously mentioned are the following:

Common Language Runtime (CLR)

Class library

The subsequent sections will discuss each of these components in detail.

Common Language Runtime

Unlike the earlier versions, in which each language had its own runtime environment, the .NET Framework provides a common runtime environment for all

.NET languages. This common runtime environment is referred to as the Common Language Runtime (CLR). Besides managing the execution of code at run time, the CLR also provides a common set of services, such as exception handling, security, and debugging, for all CLR-compliant languages. Thus, the CLR aids in implementing cross-language interoperability. The CLR manages code generated by all .NET language compilers; hence, any code that is targeted to run on the CLR is referred to as managed code. When you compile a .NET application, the associated compiler compiles the source code into managed code. The compilers generate the managed code in a language called the Microsoft Intermediate Language (MSIL, or IL for short), which is a CPU-independent language.

NOTE

MSIL includes instructions for managing the methods of an object, including the loading, initializing, and invoking of the methods, and also instructions for memory handling, exception handling, and so on.

6

Part I

INTRODUCING VC++.NET

Along with MSIL, the compiler also generates metadata that describes the code. Metadata stores details about the types, members, references, and so on. The CLR uses metadata to handle code execution and exceptions, and to provide security and cross-language integration. Both MSIL and metadata are stored in a file named the portable executable (PE) file.

After compilation, during execution of the application, the MSIL instructions, which comprise the managed code, need to be converted to CPU-dependent code. The just-in-time (JIT) compiler supplied by the CLR converts MSIL to native code, which is CPU-specific. Figure 1-1 depicts the role of the JIT compiler.

Source code

compiled

Microsoft

JIT

 

(created in Visual

into

Intermediate

compilation

Native code

Basic, Visual C++,

 

 

Language (the

 

 

 

 

 

 

 

 

and so on)

 

 

MSIL code)

 

 

 

 

 

 

 

 

FIGURE 1-1 The role of the JIT compiler

NOTE

MSIL code is similar to the byte code supported by Java. While byte code is interpreted completely when you execute an application, the JIT compiler, provided by the CLR, does not compile the entire MSIL code to the native code. Instead, a JIT compiler compiles a specific part of the code when that part of the code needs to be executed. The logic behind this implementation (referred to as on-demand compilation) is that at no point of time is the complete code required for execution. Consider an application that has two functions, one for adding data and the other for modifying data. At any point of time, the user will work with either of the two functions, because adding and modifying can’t happen in parallel. In such a case, what is the use in generating the native code for both functions? It would only result in inefficient memory usage. Hence, the JIT compiler does a partial compilation. Once compiled, the JIT compiler stores the native code generated in memory for future use. In this way, the JIT compiler ensures that your application efficiently handles the system resources.

As stated earlier, one of the most visible advantages of the CLR is its support for cross-language interoperability. In simple terms, language interoperability is the feature that enables the code written in one language to access the code written in

BASICS OF .NET FRAMEWORK

Chapter 1

7

 

 

 

another language. The component of the CLR that assists this feature is the common type system (CTS). The following section gives an overview of the CTS.

Common Type System

The CTS defines a set of standard data types and also a set of rules for creating new data types. The CTS ensures that the implementation of the standard data types, such as Integer, is the same across all CLR-compatible languages. The types supported by the CTS can be broadly classified into two categories:

Value types that directly store the data. The subcategories under this are built-in value types, user-defined value types, and enumerations.

Reference types that, unlike value types, store the reference to the memory address of the value and not the value itself. The subcategories under this are self-describing types (classes and arrays), pointer types, and interfaces.

Although the CTS facilitates language interoperability in the .NET Framework, it is not adequate to ensure a smooth interaction between applications developed using different languages (in other words, application interoperability). To accomplish this, the CLR supports yet another component, called the common language specification (CLS). The following section provides the details of the CLS.

Common Language Specification

Applications use objects to interact with each other. For a smooth interaction, you need to ensure that these objects expose only those features that are commonly understood by the languages in which they are developed. The CLS enables this by defining a set of basic language features and common type system data types that has to be implemented in the syntax of different .NET languages. Any languages that adhere to the rules of the CLS are considered CLS-compliant. CLS comes with inherent support for various types. For those types that aren’t part of CLS, hence non CLS-compliant, an alternative is readily available. For instance, the UInt32 is not CLS-compliant, and its alternative is Int32. To summarize, for any two applications to interact smoothly, the involved components or objects should be CLS-compliant.

8

Part I

INTRODUCING VC++.NET

TIP

All unsigned data types are not compliant with the CLS.

The following are some of the guidelines of the CLS:

Global static variables and methods are not allowed in a CLS-compliant language.

Some data types, such as unsigned data types, are not allowed in the CLS-compliant language.

Unique names should be used in the CLS-compliant language. Even if the language is case-sensitive, you must use distinct names for different variables, because the case-insensitive languages should be able to distinguish between the names.

The types included in a signature of a method should also be CLScompliant.

Overloading of methods is based only on the number and types of their parameters.

Pointers are not allowed in a CLS-compliant language.

You can refer to the software documentation for the other features of the CLS.

So far, you have looked at the components of the CLR that enable the features of language and application interoperability. The next section discusses one other significant feature of the CLR, automatic memory management, and the component that enables this feature, the Garbage Collector (GC).

Garbage Collector

If you have worked with C++ or Visual C++, you are well aware of “memory leakage.” Can you ever forget the “memory fault core dumped” message? You would have definitely received this message while executing applications using pointers. Although dynamic memory allocation improved your application’s efficiency in handling memory, it had one significant drawback: memory leakage.

To address memory leakage, the CLR of the .NET Framework provides the Garbage Collector, which enables automatic memory management and thus eradicates memory leakage problems. However, this feature is available only for managed code. Before the role of the GC is explained, you need to understand how

BASICS OF .NET FRAMEWORK

Chapter 1

9

 

 

 

memory allocation is done for a managed application. When you run your application, an address space is allocated for it. This address space is referred to as managed heap. When new objects are created in the application, the runtime environment allocates memory space in the address space. After the address space is filled, no more objects can be created unless some space is created. This is when the GC becomes active.

The GC scans the memory for unused objects, deletes them, and releases that memory location. To identify the unused objects, the GC tracks the roots maintained by each application. Each of these roots either points to an object in the address space or is set to null. The JIT compiler maintains a list of active roots of the application. The GC uses this list to trace the objects. The objects that are not reachable through the roots are marked as unused and are removed from the memory.

NOTE

The GC comes into action only when the address space is full. Once the cleaning is done, the GC becomes dormant.

The CLR and its constituent components and features is one of the vital components of the .NET Framework. Another prime component of the .NET Framework is the class library, discussed next.

Class Library

The class library is a part of the .NET Framework that encompasses the objectoriented classes provided by the Framework and aids you in creating Windowsbased and Web-based applications. This library is referred to as the base class library. Besides these base classes, the library also includes interfaces and value types that provide access to various system functionalities. The prominent feature of the class library is that the same library can be used for development in different languages, such as Visual C++ and Visual Basic, which was earlier impossible. This is possible because the class library provides a common set of functions that can be used across languages.

The class library of the .NET Framework is organized hierarchically into namespaces. A namespace contains the types, such as classes, that you use in an application. Therefore, based on the types required for your application, you need to import