Chapter 3. .NET Programming

Now that you know what .NET is all about, let’s talk about programming for the .NET environment. This chapter presents the common programming model that .NET provides, core languages and features that .NET supports, and language integration—how you can take advantage of object-oriented features even across different languages that .NET enables.

Common Programming Model

Without the .NET Framework, programmers must choose from amongst 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 common framework 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 have a consistent representation 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 Framework is beyond the scope of this book (see O’Reilly’s forthcoming .NET in a Nutshell series, 2001), we specifically talk about the System.Object class and present to you the major namespaces in the .NET Framework, opening the doors for you to step into this world.

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 object references and determines whether they are referring to the same object.

GetHashCode( )

Gets the object’s hash code. In .NET, 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 hash table, 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. Normally 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:	" + 
c.Equals(c)
      );
// Get the hash code from this object
      Console.WriteLine("Object hash:	" +
c.GetHashCode(  )
      );
// Use the type to obtain method information
      Console.WriteLine("Object method:	" +
c.GetType(  ).GetMethods(  )[1]
      );// Convert the object to a string
      Console.WriteLine("Object dump:	" +
c.ToString(  )
      );
    }
  }
}

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

Equivalence:    True
Object hash:    3
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.

Major Namespaces

Table 3-2 provides 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 that 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 filesystem, such as by 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, Connection, and HttpWebRequest.

System.Data

See Chapter 5.

System.Web.Services

See Chapter 6.

System.Web.UI

See Chapter 7.

System.Windows.Forms

See Chapter 8.

Again, 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 because the class and method names remain consistent across all .NET languages.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.189.227