Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C#
Namespaces

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:

Want more? Here are some additional resources on this topic:

C# Programming Guide
Namespaces (C# Programming Guide)

Namespaces are heavily used in C# programming in two ways. First, the .NET Framework uses namespaces to organize its many classes, as follows:

C#
System.Console.WriteLine("Hello World!");

System is a namespace and Console is a class contained within that namespace. The using keyword can be used so that the entire name is not required, like this:

C#
using System;
C#
Console.WriteLine("Hello");
Console.WriteLine("World!");

For more information, see the topic using Directive (C# Reference).

Second, declaring your own namespaces can help you control the scope of class and method names in larger programming projects. Use the namespace keyword to declare a namespace, as in the following example:

C#
namespace SampleNamespace
{
    class SampleClass
    {
        public void SampleMethod()
        {
            System.Console.WriteLine(
              "SampleMethod inside SampleNamespace");
        }
    }
}

Namespaces Overview

A namespace has the following properties:

  • They organize large code projects.

  • They are delimited with the . operator.

  • The using directive means you do not need to specify the name of the namespace for every class.

  • The global namespace is the "root" namespace: global::system will always refer to the .NET Framework namespace System.

Related Sections

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 9 Namespaces

See Also

Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker