[ Team LiB ] |
3.1 Common Programming ModelWithout 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.ObjectEvery 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.
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 NamespacesTable 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.
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 ] |