Freigeben über


using Directive (C# Reference)

The using directive has two uses:

  • To allow the use of types in a namespace so that 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. This is called a using alias directive.

    using Project = PC.MyCompany.Project;
    

The using keyword is also used to create using statements, which help ensure that IDisposable objects such as files and fonts are handled correctly. 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. The right side of a using alias directive must always be a fully-qualified type regardless of the using directives that come before it.

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.

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

Example 1

Description

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

Code

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 { }
        }
    }
}

Comments

A using alias directive cannot have an open generic type on the right hand side. For example, you cannot create a using alias for a List<T>, but you can create one for a List<int>.

Example 2

Description

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

Code

using System;

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

// Using alias directive for a generic class. 
using UsingAlias = NameSpace2.MyClass<int>;

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

}

namespace NameSpace2
{
    class MyClass<T>
    {
        public override string ToString()
        {
            return "You are in NameSpace2.MyClass.";
        }
    }
}

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

    class MainClass
    {
        static void Main()
        {
            AliasToMyClass instance1 = new AliasToMyClass();
            Console.WriteLine(instance1);

            UsingAlias instance2 = new UsingAlias();
            Console.WriteLine(instance2);

        }
    }
}
// Output:  
//    You are in NameSpace1.MyClass. 
//    You are in NameSpace2.MyClass.

C# Language Specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See Also

Reference

C# Keywords

Namespace Keywords (C# Reference)

Namespaces (C# Programming Guide)

using Statement (C# Reference)

Concepts

C# Programming Guide

Other Resources

C# Reference