Free mag vol1 | Page 94

CHAPTER 1  THE PHILOSOPHY OF .NET Any namespace nested within Microsoft (e.g., Microsoft.CSharp, Microsoft.ManagementConsole, Microsoft.Win32) contains types that are used to interact with services unique to the Windows operating system. Given this point, you should not assume that these types could be used successfully on other .NET-enabled operating systems such as Mac OS X. For the most part, this text will not dig into the details of the Microsoft rooted namespaces, so be sure to consult the .NET Framework 4.5 SDK documentation if you are interested.  Note Chapter 2 will illustrate the use of the .NET Framework 4.5 SDK documentation, which provides details regarding every namespace, type, and member found within the base class libraries. Accessing a Namespace Programmatically It is worth reiterating that a namespace is nothing more than a convenient way for us mere humans to logically understand and organize related types. Consider again the System namespace. From your perspective, you can assume that System.Console represents a class named Console that is contained within a namespace called System. However, in the eyes of the .NET runtime, this is not so. The runtime engine only sees a single class named System.Console. In C#, the using keyword simplifies the process of referencing types defined in a particular namespace. Here is how it works. Let’s say you are interested in building a graphical desktop application using the WPF API. While learning the types each namespace contains takes study and experimentation, here are some possible candidates to reference in your program. // Here are some possible namespaces used to build a WPF application. using System; // General base class library types. using System.Windows.Shapes; // Graphical rendering types. using System.Windows.Controls; // Windows Forms GUI widget types. using System.Data; // General data-centric types. using System.Data.SqlClient; // MS SQL Server data-access types. Once you have specified some number of namespaces (and set a reference to the assemblies that define them), you are free to create instances of the types they contain. For example, if you are interested in creating an instance of the Button class (defined in the System.Windows.Controls namespace), you can write the following: // Explicitly list the namespaces used by this file. using System; using System.Windwos.Controls; class MyGUIBuilder { public void BuildUI() { // Create a button control. Button btnOK = new Button(); ... } } 26