Chapter 1An introduction to the .net framework
What is .NET?
.NET is Microsoft’s new strategy for the development and deployment of software. Depending on your interests and development background, you may already have a number of preconceived notions regarding .NET. As we will see throughout this CodeNote:
-.NET fundamentally changes the way applications execute under the Windows Operating System.
-With .NET Microsoft is, in effect, abandoning its traditional stance, one which favors compiled components, and is embracing interpreted technology (similar, in many ways, to the Java paradigm).
-.NET brings about significant changes to both C++ and Visual Basic, and introduces a new language called C# (pronounced “C sharp”).
• .NET is built from the ground up with the Internet in mind, embracing open Internet standards such as XML and HTTP. XML is also used throughout the framework as both a messaging instrument and for configuration files.
These are all noteworthy features of .NET, or more accurately the .NET Framework, which consists of the platform and tools needed to develop and deploy .NET applications. The .NET Framework can be distilled into the following three entities:
1. The Common Language Runtime (CLR), which is the execution environment for all programs in the .NET Framework. The CLR is similar to a Java Virtual Machine (VM) in that it interprets byte code and executes it on the fly, while simultaneously providing services such as garbage collection and exception handling. Unlike a Java VM, which is limited to the Java language, the CLR is accessible from any compiler that produces Microsoft Intermediate Language (IL) code, which is similar to Java byte code. Code that executes inside the CLR is referred to as managed code. Code that executes outside its boundaries is called unmanaged code.
2. The Runtime classes, which provide hundreds of prewritten services that clients can use. The Runtime classes are the building blocks for .NET applications. Many technologies you may have used in the past (ADO, for example) are now accessed through these Runtime classes, as are basic operations such as I/O. Traditionally, every language had its own unique supporting libraries, accessible only from that particular language. String manipulation, for example, was afforded to VB programmers via the Visual Basic runtime, whereas C++ programmers depended on libraries such as STL for similar functionality. The .NET Runtime classes remove this limitation by uniformly offering services to any compiler that targets the CLR. Those familiar with Java will find the Runtime classes analogous to the Java Class Libraries.
3. Visual Studio.NET (VS.NET), which is Microsoft’s newest version of Visual Studio. VS.NET includes VB.NET, “managed” C++, and C#, all of which translate source code into IL code. VB.NET and VC.NET are the new versions of Visual Basic and Visual C++, respectively. C# is a new Microsoft language that at first glance appears to be a hybrid of C++ and Java. .NET development does not have to be limited to these languages, however. Any component or program produced by an IL-aware compiler can run within the .NET Framework. (As of this writing, other companies have announced IL compilers for Perl, Python, and COBOL.) VS.NET also comes with a fully Integrated Development Environment (IDE), which we will examine in Chapter 7. Note the VS.NET IDE now houses the development environments for both Visual C++ and Visual Basic.Outline of the Book
In this chapter we will examine the three fundamentals of the .NET Framework previously listed.
Chapter 2 provides brief installation instructions. Because .NET was still in beta release at the time of writing, these instructions may be out of date. Readers are encouraged to consult the online instructions at aNET010001.
One of the goals of .NET was to eliminate the versioning problems of traditional Win32 DLLs (a problem sometimes referred to as DLL Hell). This is realized through a new type of component in the .NET Framework called the assembly, the subject of Chapter 3. .NET eradicates DLL Hell from the Windows environment by enforcing the versioning of assemblies through public key cryptography.
In Chapter 4, we will look at some of the new language features in the .NET Framework, such as attributes, which are nonprogrammatic code statements that can be used to influence application behavior, and delegates, the new type-safe callback mechanism in the managed environment. Garbage collection, a service performed by the CLR, will also be investigated, as will reflection, the ability to ascertain type information about an application at runtime.
The .NET Framework does not preclude the use of traditional COM and Win32 components that have already been developed. Mechanisms exist to allow these “unmanaged” components (those that do not run under the auspices of the CLR) to run alongside their .NET counterparts. Such mechanisms will be investigated in Chapter 5.
Chapter 6 looks at ADO.NET, the new data access model for the .NET Framework. ADO.NET is a disconnected data access model, which means that data manipulation is performed outside the context of an open database connection. This model is especially appropriate for web applications that are loosely coupled to their data sources. As its name suggests, ADO.NET is an evolution of Microsoft’s ActiveX Data Object (ADO) model.
.NET also exposes new methodologies for developing standard Win32 applications. Windows Forms, the subject of Chapter 7, is the new way to construct desktop GUI applications for the Windows environment.
Internet developers will also welcome Web Forms, which brings the traditional ease and versatility of Visual Basic forms to Internet applications. Web Forms is a feature of ASP.NET, the topic of Chapter 8. ASP.NET is Microsoft’s new generation Active Server Page (ASP) framework for developing robust web applications.
Chapter 9 examines Web Services and an emerging communication protocol called SOAP, which allow components to interact (i.e., transfer data, perform RPC calls) via open Internet standards such as XML and HTTP.
Producing .NET applications and components requires a compiler that translates source code into IL code. VS.NET, Microsoft’s new version of Visual Studio, contains three such compilers: VB.NET, C#, and managed C++. While a full examination of these languages is beyond the scope of a CodeNote, each language is briefly discussed below. Code examples throughout this book will demonstrate some of the nuances of each language and the syntactical differences between them.
In addition to the IDE, VS.NET contains a large assortment of command line utilities. Some of these utilities are directly incorporated into the development environment, while others are stand-alone. We will examine some of these utilities throughout this CodeNote.
VB.NET is the most recent version of Visual Basic, what was once thought of as VB7. VB.NET is the first version of Visual Basic to support true object-oriented inheritance, which means it has the ability to inherit and extend the interface and behavior of any class produced by an IL compiler. This is significant, as previous versions of Visual Basic could only inherit from classes written in VB itself. This feature is not really an enhancement of VB.NET but a byproduct of the language neutrality of the CLR, which gives all IL compilers the ability to inherit classes from one another. VB.NET also includes exception handling constructs (try/catch) similar to those found in Java and C++.
Unfortunately, VB.NET also brings about some syntax changes that will break compatibility with old VB source code. Procedure parameters, for example, are now passed by value (ByVal) by default, not by reference (ByRef). Certain syntax elements such as GoSub, IsNull, and IsMissing have been removed from the language altogether. For a complete list of syntax changes, see aNET010002. Clearly, a considerable amount of effort will be required to migrate existing Visual Basic projects to the .NET Framework.
Although not exclusive to VB.NET, most VB users will be interested in the Windows Forms (Chapter 7) portion of this book, which investigates the new way Win32 screens are developed in the .NET Framework. Windows Forms completely replaces the traditional Visual Basic Forms editor. Finally, VB.NET now ships with a command line compiler (VBC.EXE), allowing one to write applications outside the development environment (using NOTEPAD, for example). In the first example in this chapter, we will investigate VB.NET’s new compiler.Managed C++
Managed C++ is a set of extensions added to the C++ language to allow one to produce “managed code” (code that executes under the auspices of the CLR). The most notable extension is the introduction of “managed types,” which shift the burden of memory management from the C++ programmer onto the CLR. Placing the __gc extension in front of the declaration of a class, for example, allows instances of the class to be garbage collected by the CLR. This convenience comes at a cost, however, as the “managed” class must adhere to the restrictions of CLR types. It cannot, for example, inherit from two classes, even though multiple inheritance is a feature of the C++ language.
Another extension is that of managed arrays, which allows these data structures to be managed by the CLR. Managed exception handling is another amendment, differing from C++ exception handling in both syntax and behavior. Examples of managed extensions can be found at aNET010003. Managed C++ is a part of Visual C++ .NET, which is the only tool in the new Visual Studio suite capable of producing unmanaged code. It is thus the only choice for producing applications that contain both managed and unmanaged code, as is discussed in the online article at aNET010010.
Excerpted from CodeNotes for .NET by Edited by Gregory Brill. Copyright © 2002 by Edited by Gregory Brill. Excerpted by permission of Random House, a division of Random House LLC. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.