C# FAQ: What is a namespace

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

Contents

What is a namespace?

A namespace is a method of organizing a group of assemblies, classes, or types. A namespace acts as a container—like a disk folder—for classes organized into groups usually based on functionality. C# namespace syntax allows namespaces to be nested.

For instance, to access the built-in input-output (I/O) classes and members, use the System.IO namespace. Or, to access Web-related classes and members, use the System.Web namespace.

All C# programs should call the System namespace—the father of all .NET Framework namespaces.

A namespace is similar to a Java package. However, whereas Java package names dictate the source files directory structure, C# namespaces dictate only the logical structure.

The C++ namespace syntax is similar to the C# syntax.

Using namespaces

Namespaces are referenced by C# applications by using the using keyword. For instance, the following example references the System namespace:

using System;

Unlike the Java language, an individual class cannot be referenced with the using keyword. A compilation error would result.

Referencing System namespace gives access to the Console class which can be used to perform console I/O. For instance, the following source code writes a line of text to the Console:

Console.WriteLine ("A console message.");

Aliases

Although the using keyword cannot be used with individual class names, aliases can! Read how to create a namespace alias.

Commonly used namespaces

The following is list of some important and frequently used .NET namespaces:

  • System.Collections
  • System.Data
  • System.Diagnostics
  • System.Drawing
  • System.IO
  • System.Net
  • System.Reflection
  • System.Runtime
  • System.Security
  • System.Threading
  • System.Web
  • System.Windows.Forms
  • System.Xml

A C# namespace is the equivalent of a Java language package.

See also


Personal tools