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

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds