C# FAQ 1.4 – How Do I Work with Namespaces?

As explained in FAQ 1.1, namespaces are placed at the top of the .NET hierarchy. Namespaces are nothing but groups of classes. Classes are also called types or assemblies. Each of the classes in a namespace can contain lots of methods.

Basically, namespaces are treated as containers for all classes. Namespaces are classified into several categories, based on the functionality of their classes. For example, if you need to work with databases, you have to call the System.Data namespace. Similarly, if you work with files, you have to call the System.IO namespace.

Namespaces in C# are similar to packages in Java, where we use a statement such as java.sql.*. Moreover, all C# programs should call the 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 namespaces should be called in your programs by applying the using keyword. For example, to call the System namespace, you have to use a statement as shown in Listing 1.4.1:

Listing 1.4.1

using System;

Notice the semicolon at the end of the statement. It is required for all statements in C#.

Note: You also should be aware that C# is a case-sensitive language, like Java.

You should not call classes with the using keyword. Hence, the code in Listing 1.4.2 results in a compilation error because Console is a class:

Listing 1.4.2

// compilation error
using System.Console;

Because the term Console is one of the classes located in the System namespace, you apply it along with its 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 1.4.3.

Listing 1.4.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 1.4.4:

Listing 1.4.4

using mydotnet = System.Console;

After that, you have to apply the alias name, mydotnet, in your C# program, as shown in Listing 1.4.5:

Listing 1.4.5

mydotnet.WriteLine("Hello C#");

You can, however, completely omit the namespace declaration (with the using keyword) in a C# program. But, as an alternative, you can refer to the namespace name in the beginning of the relevant line of each statement, as shown in Listing 1.4.6:

Listing 1.4.6

System.Console.WriteLine("Welcome to C#");

A list of .NET namespaces is shown in the following table:

System.Collections System.IO
System.Data System.Net
System.Data.OleDb System.Reflection
Stsrem.Data.SqlClient System.Runtime.InteropServices
System.Data.OracleClient System.Runtime.Remoting
System.Diagnostics System.Security
System.Drawing System.Threading
System.Drawing.Drawing2D      System.Web
System.Drawing.Printing System.Xml
System.Windows.Forms  

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read