TOP CENTER
     

Featured FAQ

What are Namespaces?
By Anand Narayanaswamy

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

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

Please refer to MSDN documentation for a complete list of all the .NET namespaces and their corresponding classes.

  New Page 1
What is Object-Oriented Programming?
What is Microsoft .NET?
What is C#?
What is Common Language Runtime (CLR)?
What do you mean by .NET Framework Class Libraries?
What are Namespaces?
What are the requirements for developing a C# application?
What are the steps involved in developing a C# application?
What will happen after the compilation of a C# program?
What are Identifiers?
What are Keywords?
How do I begin programming with C#?
What are the different editors that are available for C# programming?
How do I build a C# application using Command Line Compiler?
How do I build a C# application using Visual Studio .NET 2003?
How do I build a C# application using Visual Studio .NET "Whidbey"?