using Directive (C# Reference)
This page is specific to:.NET Framework Version:1.12.03.03.54.0
C# Language Reference
using Directive (C# Reference)

The using directive has two uses:

  • To permit the use of types in a namespace so you do not have to qualify the use of a type in that namespace:

    using System.Text;
  • To create an alias for a namespace or a type.

    using Project = PC.MyCompany.Project;

The using keyword is also be used to create using statements, which define when an object will be disposed. See using Statement for more information.

Remarks

The scope of a using directive is limited to the file in which it appears.

Create a using alias to make it easier to qualify an identifier to a namespace or type.

Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that are nested in the namespace you specify.

Namespaces come in two categories: user-defined and system-defined. User-defined namespaces are namespaces defined in your code. For a list of the system-defined namespaces, see .NET Framework Class Library Reference.

For examples on referencing methods in other assemblies, see Creating and Using C# DLLs.

Example 1

The following example shows how to define and use a using alias for a namespace:

namespace PC
{
    // Define an alias for the nested namespace.
    using Project = PC.MyCompany.Project;
    class A 
    {
        void M()
        {
            // Use the alias
            Project.MyClass mc = new Project.MyClass();
        }
    }
    namespace MyCompany
    {
        namespace Project
        {
            public class MyClass{}
        }
    }
}
Example 2

The following example shows how to define a using directive and a using alias for a class:

// cs_using_directive2.cs
// Using directive.
using System;   
// Using alias for a class.
using AliasToMyClass = NameSpace1.MyClass;   

namespace NameSpace1 
{
    public class MyClass 
    {
        public override string ToString() 
        {
            return "You are in NameSpace1.MyClass";
        }
    }
}

namespace NameSpace2 
{
    class MyClass 
    {
    }
}

namespace NameSpace3 
{
    // Using directive:
    using NameSpace1;
    // Using directive:
    using NameSpace2;   

    class MainClass
    {
        static void Main() 
        {
            AliasToMyClass somevar = new AliasToMyClass();
            Console.WriteLine(somevar);
        }
    }
}
Output

You are in NameSpace1.MyClass
C# Language Specification

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

  • 9.3 Using directives

See Also

Reference

C# Keywords
Namespace Keywords (C# Reference)
using Statement (C# Reference)

Concepts

C# Programming Guide
Namespaces (C# Programming Guide)

Other Resources

C# Reference

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View