The .NET Framework Namespaces


The .NET Framework class library is made up of a set of classes, interfaces, structures, and enumerations that are contained in almost 100 namespaces. This section begins by explaining how to use namespaces in managed C++ code and then goes on to list the major .NET namespaces, together with brief details of their function and content.

You’ve already encountered .NET namespaces in use in managed C++ code when you’ve used the C++ using keyword, as in the following example:

using namespace System::Collections;

As with traditional C++ namespaces, .NET namespaces provide an additional level of scoping that helps you organize code and guard against name clashes. Two classes with the same name can be used in a program, provided that they belong to different namespaces. A type name that includes the namespace information is called the fully qualified name, as in the examples shown here:

System::Collections::ArrayList // the ArrayList class from // System::Collections System::Threading::Thread // the Thread class from // System::Threading

Namespace names in .NET typically consist of more than one word. In managed C++, the components of the name are separated by the scope resolution operator (::). In other .NET languages such as C# and Visual Basic, the components are separated using a period (.), so in Visual Basic, the preceding examples would be:

System.Collections.ArrayList System.Threading.Thread 

All classes, interfaces, structures, and enumerations that are part of the .NET Framework class library belong to a namespace. All the namespaces provided by Microsoft begin with one of two prefixes. Those that start with System have been developed as part of the .NET Framework class library, while those beginning with Microsoft have been developed by other product groups within Microsoft.

Namespace names can have any number of components, but there’s no hierarchical relationship implied in names that contain the same root components. The hierarchical nature of namespace names simply gives you a way to organize your classes. So, for example, System::Collections::Specialized and System::Collections both contain collections, yet they aren’t necessarily related in any other way.

Note

Note to Java programmers: although .NET namespaces look very much like Java package names, there’s no relationship between namespace names and directory paths as there is in Java.

There’s no requirement that all the classes belonging to one namespace are defined in the same DLL or that a single DLL contains classes from only one namespace.

Using Namespaces in C++ Programs

Managed C++ programs use the #using preprocessor directive to import metadata into a program using the Managed Extensions for C++. Remember that metadata is information that describes the types in an assembly, and it includes the fully qualified names of all the types. For example, if the compiler sees a line such as

#using <mscorlib.dll>

it loads the DLL and reads the metadata for all the types that are defined there. Because mscorlib.dll contains most of the core .NET Framework classes, it imports the metadata for a very large number of types.

Note

You can currently only use #using to reference assemblies defined in DLLs. This restriction might be lifted at a later date, but for now, you can reference only in-process components.

The #using keyword means that you have to know which DLL holds the class or classes you want to use. Your normal source for this information will be the online help, and whenever I mention a namespace in the rest of this chapter, I’ll also say which DLL it belongs to.

Some of the fully qualified names can get rather long. Thus, it’s common to use a traditional using directive to specify namespace names so that you can use unqualified names, as shown here:

// Read the metadata for MSCORLIB #using <mscorlib.dll> // Import all the names using namespace System::Collections; // Now you can use ArrayList without having to qualify it ArrayList* pal = new ArrayList();

The System Namespace

The System namespace, defined in mscorlib.dll, contains a lot of fundamental classes, including:

  • Base classes for commonly used value and reference types, plus the base class for arrays

  • Events and event handlers

  • Delegates and interfaces

  • Attributes

  • Exceptions

  • Math

  • Application environment management

  • Garbage collection

  • Local and remote program invocation

  • Data type conversion

You’ve already met a lot of types from System in earlier chapters, and some of the other classes are rather obscure, so I won’t go through them in detail. There are a few points that are worth mentioning about some of the classes in System, and they’re covered in the following sections.

Basic Types

System implements all the basic types defined by the CTS, and you can find these listed in the following table, which you first saw in Chapter 9.

Value Type

Description

Managed C++ Equivalent Type

Byte

An 8-bit unsigned integer

char

SByte

An 8-bit signed integer

signed char

Int16

A 16-bit signed integer

short

Int32

A 32-bit signed integer

int or long

Int64

A 64-bit signed integer

__int64

UInt16

A 16-bit unsigned integer

unsigned short

UInt32

A 32-bit unsigned integer

unsigned int or unsigned long

UInt64

A 64-bit unsigned integer

unsigned __int64

Single

A single-precision 32-bit floating-point number

float

Double

A double-precision 64-bit floating-point number

double

Boolean

A Boolean value

bool

Char

A 16-bit Unicode character

wchar_t

Decimal

A 96-bit decimal value

Decimal

IntPtr

A signed integer whose size depends on the platform

No built-in type

UIntPtr

An unsigned integer whose size depends on the platform

No built-in type

Note that several of the types—namely the unsigned integer types and SByte— aren’t CLS-compliant, so be wary of using them when you’re writing code that’s going to be used from other .NET languages.

All .NET languages map these types onto native types, so managed C++ maps int onto System::Int32, but you can also use the types directly if you want.

Floating-Point Types

The Single and Double types implement IEEE-754 floating-point arithmetic. For the uninitiated, IEEE-754 floating-point arithmetic means that every operation has a defined result, so you never get a divide-by-zero error when doing floating-point math; instead, you get an answer of infinity. The floating-point classes have values to represent positive and negative infinity and “not a number,” as well as methods to test for them, as shown in the following example:

double top = 1.0; double bottom = 0.0; double result = top/bottom; if (result == Double::PositiveInfinity) Console::WriteLine(S"+infinity"); else if (result == Double::NegativeInfinity) Console::WriteLine(S"-infinity"); else if (result == Double::NaN) Console::WriteLine(S"NaN");

The Collections Namespaces

You’ve already met the collections namespaces, System::Collections and
System::Collections::Specialized, in Chapter 12. System::Collections is implemented in mscorlib.dll, while System::Collections::Specialized is implemented in system.dll, so if you want to use both, you’ll have to use two #using statements like the following:

#using <mscorlib.dll> #using <system.dll>

The following table lists the main classes that you’ll find in the System::Collections namespace.

Class

Description

ArrayList

A dynamically growable array

BitArray

A class that stores a number of Boolean values as the individual bits of an integer

Hashtable

A hash table that stores objects by hash key

Queue

A list where objects are added at one end and retrieved from the other end

SortedList

A linked list whose members are maintained in sorted order

Stack

A first-in, last-out stack

The following table shows the main classes that you’ll find in the System::Collections::Specialized namespace.

The Collections Interfaces

Class

Description

BitVector32

A class that stores a number of Boolean values as the individual bits of a 32-bit integer

ListDictionary

A dictionary implemented using a singly linked list

NameValueCollection

A sorted collection of string keys and values

StringCollection

An unsorted collection of strings

StringDictionary

A hash table with the key strongly typed to be a string rather than an object

StringEnumerator

An enumerator to work with StringCollection

The System::Collections namespace also defines a series of interfaces that are used to define the behavior of the collection classes. The collection classes themselves implement one or more of these interfaces, and you can use them as the basis for writing your own collection classes. The main interfaces are listed in the following table.

Interface

Description

ICollection

Defines the size, enumerator, and synchronization methods for all collections

IComparer

Defines a method for comparing two objects

IDictionary

Implemented by collections that manage key/value pairs, such as Hashtable and ListDictionary

IDictionaryEnumerator

Defines methods for enumerating over the items in a dictionary

IEnumerable

Defines the GetEnumerator method, which returns an IEnumerator; implemented by almost all collections

IEnumerator

Defines the properties and methods of enumerators

IHashCodeProvider

Implemented by classes that provide hash code values

IList

Implemented by classes that define indexed collections of objects

The Diagnostics Namespace

System::Diagnostics provides a number of classes to

  • Trace program execution.

  • Interact with the debugger.

  • Use the system event log.

  • Start system processes.

  • Monitor system performance.

All the classes in System::Diagnostics are implemented in system.dll.

The IO Namespace

The System::IO namespace, defined in mscorlib.dll, provides the classes that implement the .NET input/output (I/O) functionality. The main classes in this namespace are described in the following table.

Class

Description

BinaryReader

Reads .NET primitive types from a byte stream

BinaryWriter

Writes .NET primitive types to a byte stream

Directory

Contains static methods for operating on directories

DirectoryInfo

Represents a path to a directory, and contains methods for operating on the directory path

File

Contains static methods for operating on files

FileInfo

Represents a path to a file, and contains methods for operating on the file path

FileStream

Reads and writes to files using streams

FileSystemInfo

The base class for FileInfo and DirectoryInfo

FileSystemWatcher

Watches for changes in the file system and fires events when changes occur

IOException

The exception thrown when I/O errors occur

MemoryStream

Reads and writes streams of bytes to and from memory

Path

Represents directory strings in a platform-independent way

Stream

The abstract base for the stream classes

StreamReader

Reads Unicode characters from a byte stream

StreamWriter

Writes Unicode characters to a byte stream

StringReader

Reads Unicode characters from a string

StringWriter

Writes Unicode characters to a string

TextReader

The base class for StreamReader and StringReader

TextWriter

The base class for StreamWriter and StringWriter

As with all the .NET Framework class library classes, these classes are language- independent. They can be used alongside or in place of the C++ stream classes. You’ll find out more about some of the System::IO classes in Chapter 19.

The Drawing Namespaces

A number of namespaces provide all the graphics functionality for the .NET Framework:

  • System::Drawing, which encapsulates the basic GDI+ drawing functionality. The namespace provides simple two-dimensional pixel-oriented graphics.

  • System::Drawing::Design, which extends System::Drawing to add design-time functionality so that you can extend the Visual Studio .NET interface with custom items.

  • System::Drawing:Drawing2D, which provides more advanced two- dimensional and vector graphics.

  • System::Drawing::Imaging, which adds image processing functionality to GDI+.

  • System::Drawing::Printing, which allows you to customize and control the printing process.

  • System::Drawing::Text, which adds advanced typography support to GDI+, including the ability to create and use collections of fonts.

The original set of Windows graphics routines was called GDI, for Graphical Device Interface. It provided a very simple set of two-dimensional graphics primitives, so you could draw lines, circles, rectangles, and other simple shapes, as well as strings of text. The .NET Framework has built on this functionality, and its graphics library is called GDI+. You’ll learn more about GDI+ in Chapter 18.

The Forms Namespace

Visual Basic programmers have been used to programming with forms for some time. GUI applications in Visual Basic consist of a number of forms, each of which is a separate top-level window. Developers select controls—such as buttons and list boxes—from the toolbox and drop them onto the form. Every control has properties and methods associated with it; the Visual Basic Property Editor allows interactive modification of properties at design time, and both methods and properties can be accessed from code at run time.

The System::Windows::Forms namespace, which you’ll learn more about in Chapter 16 and Chapter 17, provides Visual Basic-style form-based development to all .NET languages, including C++. This namespace is huge, containing over 300 classes, structures, and enumerations. The following table lists some of the most important classes to give you a feel for what is available.

Class

Description

Application

Provides methods and properties for managing applications

AxHost

Wraps Microsoft ActiveX controls so that they can be used as Windows Forms controls

Button

Represents a Windows button control

CheckBox

Represents a Windows check box control

CheckedListBox

Represents a list box control that has a check box to the left of each item

Clipboard

Gives access to the system clipboard

ColorDialog

Displays a standard color picker dialog box

ComboBox

Represents a Windows combo box control

Control

The base class for all controls

Cursor

Represents a cursor

DataGrid

Displays Microsoft ADO.NET data in a scrollable grid

DateTimePicker

Represents a Windows date-time picker control

DomainUpDown

Represents a Windows up-down control that displays string values

FileDialog

The base class to display the standard File Open or File Save dialog boxes

Form

Represents a window or a dialog box that makes up part of an application’s user interface

Label

Represents a Windows label control

ListBox

Represents a Windows list box control

ListView

Displays a list of items in one of four views

Panel

Represents a Windows panel control

RichTextBox

Represents a Windows rich text box control

StatusBar

Represents a Windows status bar control

TextBox

Represents a Windows text box control

ToolBar

Represents a Windows tool bar control

The Net Namespaces

Networking support is provided by the System::Net and System::Net::Sockets classes. System::Net provides an interface to many of the protocols commonly used today, such as manipulating IP addresses, making DNS lookups, talking to HTTP and FTP servers, managing cookies, and authentication.

System::Net::Sockets provides an implementation of the Berkeley Sockets protocol and provides a .NET wrapper around the Windows WinSock API.

The Xml Namespaces

XML is heavily used throughout the .NET Framework, and several namespaces provide support for creating and manipulating XML:

  • System::Xml, which provides the basic classes needed for processing XML

  • System::Xml::Schema, which provides support for XML Schemas

  • System::Xml::Serialization, which lets you serialize .NET objects to and from XML

  • System::Xml::XPath, which contains the XPath parser and evaluation engine

  • System::Xml::Xsl, which contains the Extensible Stylesheet Language (XSL) processor

Using these classes, it’s possible to perform all the manipulation of XML that you’ll ever need to do. These classes make the .NET Framework one of the most productive environments for XML programming.

The Data Namespaces

The System::Data namespaces hold the classes that implement ADO.NET, a new version of Microsoft Active Data Objects technology optimized to work with the .NET Framework, which enables you to build components that manage data from a number of data sources. Data from different data sources is provided by data providers, of which there are three shipped with the .NET Framework. The .NET Framework Data Provider for OLE DB uses Microsoft COM-based technology that makes it possible to use many different kinds of data sources—such as relational database tables, Microsoft Excel spreadsheets, and even text files—as if they were databases. The .NET Framework Data Provider for SQL Server works with data from Microsoft SQL Server. Finally, the .NET Framework Data Provider for Oracle makes it possible to work with Oracle databases from .NET code.

The most important class in the System::Data namespaces is DataSet, which represents an in-memory cache of data retrieved from a data source. A DataSet consists of one or more DataTable objects, and these in turn consist of a collection of DataColumn objects.

The Web Namespaces

Because one of the main reasons for introducing the .NET Framework was to make it easier to build Web applications, it’s perhaps no surprise that the .NET Framework contains a number of namespaces related to Web programming. These are all related to Microsoft ASP.NET, the latest version of Microsoft Active Server Pages technology that is optimized to work in the .NET environment.

The most significant of the Web namespaces are listed here:

  • System::Web, which provides the basic functionality for browser-to- server communication over HTTP, including the HttpRequest and HttpResponse classes that enable an ASP.NET page to exchange data with the client using HTTP

  • System::Web::Mail, which lets you prepare and send e-mail attachments using the SMTP service built into Windows 2000, Windows XP, and Windows NT

  • System::Web::Security, which provides classes that implement security in ASP.NET

  • System::Web::Services, which provides the classes that let you build Web services

  • System::Web::UI, which contains all the classes that let you build server-side controls

The features provided by two of these namespaces merit particular mention. Web services, in particular, are a great new feature introduced by the .NET Framework. A Web service is a programmable entity living on a Web server that can be accessed using standard Internet protocols. What this means in practice is that you can expose a function on a Web server that others can call. Communication between client and server uses standard protocols, such as HTTP, and data is usually passed to and from the Web service in XML format using SOAP. The use of XML over HTTP makes it possible to access Web services easily from clients written in just about any programming language on any platform. It’s also possible to find out what services a Web server supports, and it’s very easy in Visual Studio .NET to write clients that make use of Web services.

The System::Web::UI namespaces let you build server-side controls. You program these as if they were normal controls, but their code executes on the server. The System::Web::UI::HtmlControls namespace contains classes that represent HTML server controls that map directly onto standard HTML elements, such as buttons and forms. System::Web::UI::WebControls is more abstract and lets you program server-side controls that might not map directly onto HTML.




Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
EAN: N/A
Year: 2003
Pages: 208

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net