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  


About the Author

Anand Narayanaswamy

Anand Narayanaswamy (Microsoft MVP) is a freelance writer for Developer.com and Codeguru.com. He works as an independent consultant and runs NetAns Technologies (http://www.netans.com)which provides affordable web hosting services. He is the author of Community Server Quickly (http://www.packtpub.com/community-server/book). Anand also runs LearnXpress.com (http://www.learnXpress.com) and Dotnetalbum.com (http://www.dotnetalbum.com) and regularly contributes product and book reviews for various websites. He can be reached at ananddotnet@yahoo.co.in

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • The first phase of API management was about realizing the business value of APIs. This next wave of API management enables the hyper-connected enterprise to drive and scale their businesses as API models become more complex and sophisticated. Today, real world product launches begin with an API program and strategy in mind. This API-first approach to development will only continue to increase, driven by an increasingly interconnected web of devices, organizations, and people. To support this rapid growth, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds