Monday, November 22, 2010

C# Namespaces

With all of the classes provided in the .NET Framework, it’s easy to get confused about which classes perform which functions and the methods that should be used from particular classes. To help simplify things, Microsoft uses namespaces in classifying .NET Framework classes.

What Are Namespaces?

C# application consists of one or more classes. Each class defines an object that can contain data and methods to manipulate the data. At least one class in each application must contain a program interface method called Main(). The Main() method lets the C# compiler know where to begin execution of the program. Other classes can be defined within the program (such as the DataClass), or can even be shared with other programs.
       C# namespaces are used to identify a higher-level hierarchy of class names, allowing you to group similar classes together within a single namespace. The namespace is defined in the source code file before the class definition, using the namespace directive:
namespace Test1;
class testProgram
{
}
namespace Test2;
class testProgram
{
}
For programs that do not declare a namespace (such as the SampleClass program) the defined classes become part of a global namespace. These classes are globally available to any application in the CLR.
Each namespace uniquely identifies the programs within it. Notice that both of the sample namespaces just shown contain a class called testProgram; most likely they perform separate functions. If your program needs to use one or both of the testProgram classes, you must specify which class you mean to use by referencing the namespace.

.NET Framework Namespaces
The .NET Framework uses namespaces to help categorize library classes used in the CLR. This helps programmers determine the location of various classes and how to define them in their programs.
Many .NET Framework namespaces make up the core CLR classes. Table 1.3 lists some of the common namespaces you will encounter in your C# network applications.
Table 1.3: .NET Framework Class Namespaces
Namespace
Description of Classes
Microsoft.Win32
Handles events raised by the OS and Registry handling classes
System
Base .NET classes that define commonly used data types and data conversions
System.Collections
Defines lists, queues, bit arrays, and string collections
System.Data
Defines the ADO.NET database structure
System.Data.OleDb
Encapsulates the OLE DB .NET database structure
System.Drawing
Provides access to basic graphics functionality
System.IO
Allows reading and writing on data streams and files
System.Management
Provides access to the Windows Management Instrumentation (WMI) infrastructure
System.Net
Provides access to the Windows network functions
System.Net.Sockets
Provides access to the Windows sockets (Winsock) interface
System.Runtime.Remoting
Provides access to the Windows distributed computing platform
System.Security
Provides access to the CLR security permissions system
System.Text
Represents ACSII, Unicode, UTF-7, and UTF-8 character encodings
System.Threading
Enables multi-threading programming
System.Timers
Allows you to raise an event on a specified interval
System.Web
Enables browser and web server functionality
System.Web.Mail
Enables sending mail messages
System.Windows.Forms
Creates Windows-based application using the standard Windows graphical interface
System.XML
Provides support for processing XML documents

Using Namespaces in Programs

An easier way is to declare the namespace with the C# using directive at the beginning of the program. Any classes contained within a namespace declared with using do not have to be referenced by their namespace name:
using System;
Console.WriteLine("The result is {0}", sample.addem());
The C# compiler searches all declared namespaces for the Console class and automatically references the proper namespace.

1 comment: