The .NET Framework class library is organized into a hierarchy of namespaces. Each namespace can contain types, such as classes and interfaces, as well as other subordinate namespaces. The root namespace is System, and every .NET Framework application will use some of the types it contains. Yet the types in several other namespaces are also likely to be commonly used by a broad swathe of developers. System is the foundation, but it's by no means the whole story. The library is a hierarchy of namespaces |
Perspective: How Big Is Big? If there's any one fact about the .NET Framework class library that you should internalize, it is that the library is bigvery, very big. Many, many people have been working on the .NET Framework for several years, and a large chunk of them were designing and building this class library. The steepest learning curve for developers moving to .NET will be learning the .NET Framework class library. Fortunately, you don't need to learn the whole thing. Unless you have a great deal of free time and remarkably catholic interests, you're unlikely ever to understand every type in the library. Instead, any developer working in the .NET world should first decide which parts of this mountain of software she absolutely needs to understand and then determine which parts she's really interested in. Every developer will need to understand some of its namespaces, but most will be able to ignore many others quite safely. Providing a large set of generally useful code is clearly a good idea. While the initial .NET Framework class library wasn't exactly on the mark in every waythe 2005 release of version 2.0 includes changes big and smallits straight forward design makes it as approachable as something this big can ever be. |
The System Namespace The System namespace is the ultimate parentthe rootof the .NET Framework class library. Along with its large set of subordinate namespaces, System itself contains many different types. Among the most interesting of these are the following: The core types defined by the CLR's Common Type System, including Int16, Int32, Char, Boolean, and all other standard value types, along with reference types such as Array and Delegate. The fundamental base type Object is also defined here. Console, the class whose WriteLine method was used in the previous chapter to output simple information. This class also provides a corresponding ReadLine method and several others. Math, the class whose Sqrt method was used in the previous chapter to compute the square root of a number. This class has more than two dozen methods that provide standard ways to compute sines, cosines, tangents, logarithms, and other common mathematical functions. Environment, a class used to access information about the environment of the currently running application. An application can learn what its current directory is, find out what operating system it's running on, determine how much memory it's using, and more. GC, a class used to affect how and when garbage collection happens. By invoking this class's Collect method, an application can force garbage collection to happen immediately. (This isn't likely to be a good idea, however, since the CLR knows better than you when garbage collection should occur.) Random, a class whose members can be used to compute pseudorandom numbers. System is the root namespace of the .NET Framework class library |
Except for the base CLR types, the types in System sometimes seem to have been placed here because there was no obviously better namespace for them. Still, these types can be useful in a broad range of applications. The types in System are a diverse lot |
A Survey of System's Subordinate Namespaces Directly below System are more than two dozen other namespaces, many of which have subnamespaces of their own. Providing even a short survey of these is a daunting task. Nonetheless, before moving on to examine the most important namespaces in this and later chapters, it's important at least to attempt a broad view. With this lofty goal in mind, this section takes an alphabetical look at most of the namespaces directly below System, providing a brief description of what each one offers. Note that this chapter (and, in fact, this book) doesn't describe every namespace in this library, and so what follows isn't a complete list. System directly contains more than two dozen namespaces |
System.CodeDom includes types that can be used to create a program directly in memory. This large set of types includes classes for generating declarations, assignments, if statements, and everything else required to construct a complete program. Even comments are supported. Once a program is created, it can be output in C#, Visual Basic (VB), or some other language. The types in this namespace can be used by any .NET developer, and they're also used by some parts of the .NET Framework itself, such as ASP.NET. System.CodeDom provides a way to generate code for CLR-based programs |
System.Collections includes types for creating and working with hash tables, arrays, queues, stacks, lists, and other generally useful data structures. These types are defined quite generally. For example, the Stack and Queue classes contain Objects, which lets them contain values of any CTS type. A subordinate namespace, System.Collections.Generic, contains essentially the same group of types, this time defined using generics rather than simple Objects. System.Collections also contains the subordinate namespace System.Collections.Specialized, which provides types for more narrowly applicable uses, such as a collection of Strings (although the types in System.Collections.Generic effectively make many of these specialized collection types redundant). System.Collections defines generic types such as stacks and queues |
System.ComponentModel contains types for creating various kinds of .NET Frameworkbased components. Among the types it includes is the Component class, which serves as the basis for components used by Windows Forms, described in this chapter, and for many other classes in the .NET Framework class library. Components, which implement the IComponent interface defined in this namespace, exist inside instances of the Container class, also defined in this namespace. Each Container object implements two more interfaces defined in this namespace: one occurrence of the IContainer interface, along with one ISite interface for each component the container hosts. This namespace also contains types for licensing components. If you're familiar with the older COM-based mechanisms for building ActiveX controls, the types defined in this namespace should suggest those once-popular models for building components. System.Component-Model provides a foundation for building software components |
System.Configuration provides types such as the ConfigurationsSettings class that allow accessing configuration information for a .NET Frameworkbased application. It also contains subordinate namespaces such as System.Configuration.Assemblies for working with assembly-specific configuration information and System.Configuration.Install for building custom installers for CLR-based software. System.Configuration supports configuring assemblies and creating installers |
System.Data is among the most important namespaces in the .NET Framework class library. The types in this namespace implement ADO.NET, the standard approach to accessing data for .NET Framework applications. Its subordinate namespaces include System.Data.SqlClient, which allows access to data stored in Microsoft's SQL Server, System.Data.OracleClient, which allows access to data stored in an Oracle database, System.Data.OleDb, which allows access to data sources using OLE DB providers, and System.Data.Odbc, which allows access to data sources through ODBC. ADO.NET is described in more detail in Chapter 6. System.Data contains the types that make up ADO.NET |
System.Diagnostics contains a large set of classes, interfaces, structures, and other types that help with debugging .NET Framework applications. For example, the Trace class allows adding assertions to code that verify key conditions, writing messages that trace the flow of execution, and performing other useful functions in released software. The Debug class, also defined in this namespace, provides similar services but is designed to be used during development rather than in a released product. System.Diagnostics supports tracing, assertions, and more |
System.DirectoryServices contains types for accessing Active Directory and other directory services. Prior to .NET, the standard way to expose Windows services was through COM-based interfaces. Active Directory, for instance, can be accessed via the Active Directory Services Interface (ADSI), which is defined as a collection of COM objects. The .NET Framework has superseded COM for new interface definitions, however, and so new ways to expose services must be created using managed code. The types in the System.DirectoryServices namespace are the .NET Framework's analog to ADSI. A subordinate namespace, System.DirectoryServices.Protocols, provides an interface for accessing directories using the standard Lightweight Directory Access Protocol (LDAP). System.Directory-Services provides an API to Active Directory |
System.Drawing provides a large set of types for using the services of the current version of Microsoft's Graphics Device Interface (GDI), known as GDI+. System.Drawing itself includes classes for working with pens, brushes, and other drawing tools, while several subordinate namespaces contain types for related uses. System.Drawing.Drawing2D, for example, contains types for vector graphics and other two-dimensional drawing functions; System.Drawing.Imaging contains types for working with metafiles and other more advanced GDI imaging; System.Drawing.Printing contains types for controlling printers; and System.Drawing.Text contains types for manipulating fonts. A System.Drawing.Design namespace is also included that provides types for customizing the user interface developers see at design time. System.Drawing supports creating text and several kinds of graphics |
System.EnterpriseServices contains types for accessing the services provided by COM+, including support for distributed transactions, role-based authorization, and object pooling. Unlike most of the functions provided by the .NET Framework class library, the types contained here largely provide a wrapper around the existing COM+ software rather than reimplementing it as managed code. This important namespace is described in more detail in Chapter 7. System.Enterprise-Services allows access to COM+ services |
System.Globalization contains types for creating national calendars, converting to national code pages, formatting dates and times, and other aspects of building software that supports multiple cultures. Globalized software is important, and so many of the .NET Framework's basic functions are automatically culture-aware. For example, conversion to a currency value can automatically examine the caller's culture setting to format the currency appropriately. System.Globalization helps developers write software that works in diverse cultures |
System.IO provides a large set of types for reading and writing files, directories, and in-memory streams. While access to a database management system (DBMS) is often the main route applications take to data, the ability to work with files is still important. System.IO also contains a few subordinate namespaces that provide related services. System.IO.Compression, for example, allows reading and writing compressed data using the GZIP standard. The basics of System.IO are described later in this chapter. System.IO supports access to files and directories |
System.Management provides types for accessing Windows Management Instrumentation (WMI) data from managed code. WMI is Microsoft's implementation of the Web-Based Enterprise Management (WBEM) initiative supported by many different vendors. A number of the types in this namespace provide support for the WMI Query Language (WQL), a dialect of SQL focused on accessing WMI-related information. System.Management supports working with WMI data |
System.Media includes classes for working with sounds. For example, this simple namespace provides a SoundPlayer class with methods such as Play and Stop, allowing a CLR-based application to start and stop playing of a sound file. System.Media provides a simple way to play sounds |
System.Messaging contains types for accessing Microsoft Message Queuing (MSMQ), Microsoft's solution for Windows-to-Windows message queuing. MSMQ has several other application programming interfaces (APIs), including a COM-based API and an API defined as a set of C function calls. System.Messaging's types define the MSMQ API that managed code should use. System.Messaging provides an API for MSMQ |
System.Net provides types for accessing several common protocols, including HTTP, FTP, and the Domain Name System (DNS). It also contains the abstract classes WebRequest and WebResponse, which allow building applications that are unaware of what protocol is being used to communicate. These applications can simply make requests to and get responses from specified URLs and let the underlying software worry about the details. Version 2.0 of the .NET Framework adds the HttpWebListener class, which allows a developer on newer versions of Windows to create simple Web servers. System.Net also contains a number of subordinate namespaces. System.Net.Sockets, for example, provides a managed implementation of the traditional sockets interface to TCP and UDPit's WinSock for the .NET generationwhile System.Net.Mail allows sending mail to an SMTP server. System.Net supports access to HTTP, TCP, and other protocols |
System.Reflection contains a large set of types for examining an assembly's metadata. A subordinate namespace, System.Reflection.Emit, contains types that can be used to create other types dynamically. Reflection is described in more detail later in this chapter. System.Reflection allows access to an assembly's metadata |
System.Resources provides types that allow managed code to work effectively with resources. Resources are parts of an application that can be separated from the source code, such as message strings, icons, and bitmaps. A primary use of this namespace's types is to allow an application to display different resources easily when used in different cultures. This makes the contents of this namespace especially relevant for internationalized software that must work in many different countries and languages. System.Resources allows manipulating resources |
System.Runtime is a parent namespace that contains several important subordinate namespaces. One of these, System.Runtime.InteropServices, is one of the .NET Framework class library's most important namespaces. It contains types that help in interoperating with non-CLR-based software, such as COM classes, and is described in more detail later in this chapter. System.Runtime.Remoting is another important namespace, as the types it contains allow accessing managed objects in other processes and other machines. This technology, known as .NET Remoting, is described in more detail in Chapter 7. System.Runtime.Serialization contains types for serializing a managed object's state. Serializing a managed object means copying its state (although not its code) into either memory or some more permanent medium such as a file. The ability to work with an object's state in this way is a fundamental feature of the .NET Frameworkit's used by .NET Remoting, for exampleand this namespace is also described in more detail later in this chapter. System.Runtime provides interoperability, remoting, serialization, and other fundamental services |
System.Security contains classes, interfaces, enumerations, and subordinate namespaces that provide various security-related functions. The namespace directly contains several fundamental classes, such as the SecurityManager class, which is the primary access point for working with the security system. The subordinate namespace System.Security.Cryptography contains types for using secret and public key cryptography services. Those types provide access to the Windows Cryptographic Service Providers (CSP) that actually implement algorithms such as DES, RC2, and RSA. System.Security.Cryptography itself also contains a few subordinate namespaces. One of them, System.Security.Cryptography.X509Certificates, contains classes for creating and using X.509 version 3 public key certificates for use with Microsoft's Authenticode technology, while another, System.Security.Cryptography.Xml, implements the World Wide Web Consortium (W3C) standard for digitally signing data described using XML. System.Security provides cryptography support and other security services |
System.Security also contains a few other subordinate namespaces. They include System.Security.Permissions, which defines types such as a class representing each of the possible permissions for code access security, and System.Security.Policy, which defines classes such as Site, URL, Publisher, Zone, and others used in defining security policy. How these concepts are used by the CLR was described briefly in Chapter 2's section on code access security. Another child namespace of System.Security, System.Security.Principal, contains types for working with security principals. These classes are used by the CLR in implementing the role-based security described in Chapter 2. System.ServiceProcess contains types for building .NET Framework applications that run as long-lived processes called Windows services. This kind of application was previously known as an NT service and is also sometimes referred to as a daemon. System.ServiceProcess allows creating Windows services |
System.Text contains a group of classes for working with text. For example, this namespace's UTF8Encoding class can convert Unicode characters from their default encoding into UTF-8 and vice versa. UTF stands for Unicode Transformation Format, and UTF-8 is compatible with the familiar ASCII character representation (although it allows representing multibyte non-ASCII characters, too). System.Text also contains the namespace System.Text.RegularExpressions. The types in this namespace allow access to a generic regular expression engine that can be used from any CLR-based language. System.Text supports text conversion and working with regular expressions |
System.Threading provides standard threading services used by all CLR-based languages |
System.Threading is another of the .NET Framework's more important namespaces. The types it contains provide a standard way for developers working in any .NET language to build multithreaded applications. In the pre-.NET era, VB, C++, and other languages all had their own unique approach to threading, with COM's apartments serving as a (complicated) cross-language solution. With the .NET Framework, all CLR-based languages can use the contents of System.Threading to work with threads in a consistent way. Perhaps the most important type in this namespace is the Thread class, which provides methods to start a thread, stop one, cause it to wait for another thread, and more. The namespace also contains classes for using fundamental synchronization constructs, such as mutexes and monitors, and for working with a thread pool. System.Timers contains types for specifying and handling recurring events. The most important class in this namespace is Timer, which allows a developer to specify an interval at which an Elapsed event, defined as part of the Timer class, will be raised in his application. The application can then catch this event and perform some function. For example, an application may wish to check for new mail once every ten minutes. Using this mechanism, the developer could cause the Elapsed event to take place every ten minutes, then put mail-checking code in the handler for this event. System.Timers supports working with regularly occurring events |
System.Transactions, a new namespace in version 2.0 of the .NET Framework, contains types that let applications use transactions. Prior to this 2005 release, applications could use ADO.NET for handling database transactions or Enterprise Services for working with more complex, multisystem transactions. With System.Transactions, Microsoft rethought how applications should use transactions on Windows. This important new technology is described in more detail later in this chapter. System.Transactions provides support for applications using transactions |
System.Web implements ASP.NET and ASP.NET Web services |
System.Web is, after System itself, perhaps the most important namespace in the .NET Framework class library. Comprising many types and many subordinate namespaces, the software it contains implements ASP.NET. The two most important children of System.Web are System.Web.UI, which contains types for building browser-accessible applications, and System. Web.Services, which contains types for creating applications that expose Web services. ASP.NET's support for creating browser-based applications is described in Chapter 5, while Chapter 7 looks at the support it provides for Web services. Perspective: The Risks and Rewards of a Standard Class Library The goal of creating a class library is to make life easier for developers. Rather than reinvent wheels from scratch, a developer can reuse existing wheels. Class libraries aren't a new idea, and several useful collections of code exist today. Before the arrival of .NET, the most popular was surely the large set of packages defined for the Java environment, which collectively define a set of services that look much like the .NET Framework class library. (In fact, a Java package is very similar to a namespace in the .NET Framework.) The existence of this large set of prepackaged functionality was a major reason for Java's success. There have been attempts to create broad class libraries that were doomed by their own ambition, however. One of the most visible (and probably most expensive) of these was a project undertaken by the joint Apple/IBM venture Taligent. I once gave a seminar for the technical staff at Taligent, and I don't think I've ever spoken to a more intelligent audiencethose people were amazing. Yet in part because the class libraries they produced were so complex and had such a long learning curve, they ultimately weren't successful. Does the .NET Framework class library suffer from this problem? I don't think so. It's certainly big, but a developer doesn't need to understand the whole thing to use just one part of it. Some degree of compartmentalization is critical to a successful class library, since the number of people willing to devote the necessary time to achieve complete mastery is much smaller than those who can benefit from exploiting some parts of it. The success of the .NET Framework suggests that the creators of its library made mostly the right choices. Overall, the .NET Framework class library appears to strike a good balance between power and complexity. While the library certainly requires effort to understand, the popularity of .NET today suggests that developers believe the payoff is worth it. |
System.Windows.Forms contains types used to construct local Windows Graphical User Interfaces (GUI). Local GUIs are somewhat less important for application developers than they once werea browser interface is now more commonbut they're nonetheless an important topic. Accordingly, this namespace is described in more detail later in this chapter. System.Windows.Forms supports building local Windows GUIs |
System.XML contains types useful for working with XML documents. The .NET Framework is shot through with support for XML, and the contents of this namespace provide a significant amount of technology for developers working with XML-defined data. This namespace is also described in more detail later in this chapter. System.XML includes a wide range of support for working with XML-defined data |
Code provided by a standard library isn't useful unless you know it's there. Accordingly, while memorizing the entire .NET Framework class library isn't necessary, any developer writing CLR-based code should have a broad grasp of what the library makes available. One way to think about what's available is to group the library's services into a few broad categories: Fundamental services such as performing basic input and output, serializing an object's state, accessing metadata via reflection, working with XML, using transactions, interoperating with other Windows software, and building native Windows GUIs. Services for creating Web applications that interact with people. This critically important area is addressed by the browser-oriented aspects of ASP.NET. Services for working with data. This mostly means support for working with data in DBMSs, such as that provided by ADO.NET, but XML data is also important. Services for creating distributed applications. This includes the Web services aspects of ASP.NET, along with .NET Remoting and Enterprise Services. Every .NET Framework developer needs a basic knowledge of the class library |
The rest of this book is structured in this way. The remainder of this chapter describes some of the library's most important general services, while the chapters that follow examine the other three categories. |