DekGenius.com
[ Team LiB ] Previous Section Next Section

3.1 Common Programming Model

Without the .NET Framework, programmers must choose from a wealth of APIs or libraries that support system services. For example, if you want to write GUI applications on Windows, you have a slew of options from which to choose, including the Win32 API, MFC, ATL, VB, and so on. Once you've chosen the library, you have to learn how to use the structures, classes, functions, interfaces, and so forth that the library provides. Unfortunately, this knowledge doesn't transfer directly into a different environment. For instance, there's a big difference between the code to manage IO in MFC and the code to manage IO in VB.

One of the goals of the .NET Framework is to bring commonality to application development by providing a framework of common classes to developers who are using compilers that generate IL. This set of classes, known as the Base Class Library (BCL), is extremely helpful: if you know how to take advantage of IO functionality in .NET using your favorite language, you can easily port that code to another language. This is possible because the namespaces, classes, methods, and so forth are equally accessible in all languages. For example, you can output a line of text to the console the same way across all .NET languages by using the WriteLine( ) method of the Console object, as we have seen elsewhere in this book. This consistent framework requires less development training and enables higher programmer productivity.

Since a full discussion of the entire set of classes in the .NET BCL is beyond the scope of this book (see O'Reilly's In a Nutshell .NET series), we talk about the System.Object class and present the major namespaces in the .NET Framework, opening the doors for you to step into this world.

3.1.1 System.Object

Every type in .NET is an object, meaning that it must derive directly or indirectly from the Object class. If you don't specify a base class when you define a class, the compiler will inject this requirement into the IL code. The Object class supports a commonality that all .NET classes inherit and, thus, automatically provide to their consumers. The Object class exposes the public methods listed in Table 3-1, which you can invoke on any given .NET object at runtime.

Table 3-1. Public methods of the Object class

Methods

Description

Equals( )

Compares two objects and determines whether they are equivalent (having the same content).

ReferenceEquals( )

Compares two object references and determines whether they are referring to the same object in memory.

GetHashCode( )

Gets the object's hash code. Hash codes are used as an added mechanism for determining object uniqueness at runtime. For instance, if you want your objects to be used as keys in a hashtable, you must override this function and provide a unique hash value for each instance of your class.

GetType( )

Obtains the object's type at runtime. Once you have obtained the object's type, you can obtain everything about that type using the Reflection API, as explained in Chapter 2.

ToString( )

Gets a string representation of the object. Often used for debugging purposes, this method spits out the fully qualified class name by default.

Examine the following program, which illustrates the use of all these methods:

using System;
namespace Cpm
{
  class CPModel
  {
    public static void Main(  ) 
    {
      CPModel c = new CPModel(  );

      // Test for self equivalence.
      Console.WriteLine("Equivalence:\t" + 
        c.Equals(c)
      );

      // Get the hash code from this object.
      Console.WriteLine("Object hash:\t" + 
        c.GetHashCode(  )
      );

      // Use the type to obtain method information.
      Console.WriteLine("Object method:\t" + 
        c.GetType(  ).GetMethods(  )[1]
      );

      // Convert the object to a string.
      Console.WriteLine("String representation:\t" + 
        c.ToString(  )
      );
    }
  }
}

If you compile and run this C# program, you get the following output:

Equivalence:    True
Object hash:    2
Object method:  Boolean Equals(System.Object)
Object dump:    Cpm.CPModel

The boldface line displays the second method of the CPModel class. If you look back at the program's code, you'll see that we use the GetType( ) method to get the type, and then we use the GetMethods( ) method to retrieve the array of methods supported by this type. From this array, we pull off the second method, which happens to be Equals( ), a method that's implemented by System.Object.

As you can see, the System.Object class provides a mechanism for runtime type identification, equivalence, and inspection for all .NET objects.

3.1.2 Major Namespaces

Table 3-2 is a short list of important namespaces and classes in the .NET Framework that provide support for almost any application that you will develop. These are the namespaces that you'll find yourself using again and again the more you develop .NET applications. For more information, consult MSDN Online or your SDK documentation, as a detailed discussion of these namespaces and classes is beyond the scope of this book.

Table 3-2. Important .NET namespaces and classes

Namespace

Description

System

Includes basic classes almost every program will use. Some simple classes that belong in this namespace are Object, Char, String, Array, and Exception. This namespace also includes more advanced classes such as GC and AppDomain.

System.IO

Provides a set of classes to support synchronous and asynchronous IO manipulation for data streams. Also provides classes that allow you to manipulate the file system, such as creating, managing, and deleting files and directories. Some of these classes are FileStream, MemoryStream, Path, and Directory.

System.Collections

Includes a set of classes that allow you to manage collections of objects. Some of these classes are ArrayList, DictionaryBase, Hashtable, Queue, and Stack.

System.Threading

Includes a set of classes that support multithreaded programming. Some of these classes are Thread, ThreadPool, Mutex, and AutoResetEvent.

System.Reflection

Includes a set of classes that support dynamic binding and type inspection. Some of these classes are Assembly, Module, and MethodInfo.

System.Security

Includes a set of classes and child namespaces that provide security support. The interesting child namespaces include Cryptography, Permissions, Policy, and Principal.

System.Net

Includes a set of classes and child namespaces that provide support for network programming. Some of these classes are IPAddress, Dns, and HttpWebRequest.

System.Data

Contains classes for ADO.NET. See Chapter 5.

System.Web.Services

Contains classes for XML web services. See Chapter 6.

System.Web.UI

Contains classes for ASP.NET web pages. See Chapter 7.

System.Windows.Forms

Contains classes for Windows user interface applications. See Chapter 8.

Keep in mind that if you know how to use any of the classes in these namespaces, you can write the code to take advantage of them in any language that targets the CLR, because the class and method names remain consistent across all .NET languages.

    [ Team LiB ] Previous Section Next Section