Namespaces
are nothing but group of classes or types or
assemblies. Each of these classes contains lot of methods. Basically,
namespaces are treated as containers for all classes and are classified
into several categories, based on its functionalities. For example, if
you need to work with databases, you have to call the namespace System.Data. Similarly, if you are working with files you have to call
System.IO namespace.
A namespace in C# is very similar to packages in Java, where we will use a
statement like java.sql.* Moreover, all C# programs should call System
namespace. This is the root of all other namespaces in the .NET
framework.
You have to apply the namespaces by following certain conventions as
laid out by the .NET framework. All C# namespaces should be called in your
programs by applying the keyword using. For example, to call System
namespace you have to use a statement as shown in listing 6.1:
Listing 6.1
using System;
Notice the semicolon at the end of the statement. It is required for all
statements since C# is a case sensitive language like Java. You should
not call classes with the using keyword. Hence the code in listing 6.2
results in compilation error:
Listing 6.2
// compilation error
using System.Console;
Since the term Console is one of the classes located in the System
namespace, you should apply it along with built-in methods. For example,
you can write text to the command prompt by using the WriteLine() method
of the Console class as shown in listing 6.3
Listing 6.3
Console.WriteLine(“Hello World”);
Even though you cannot directly apply the class names along with the
using directive, you can create an alias as shown in listing 6.4:
Listing 6.4
using mydotnet = System.Console;
After that you have to apply the alias name, mydotnet, in your C#
program as shown in listing 6.5:
Listing 6.5
mydotnet.WriteLine("Hello C#");
A partial list of .NET namespaces are shown below for your reference