Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Thenamespace keyword

  • 2024-11-22
Feedback

In this article

Thenamespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.

namespace SampleNamespace{    class SampleClass { }    interface ISampleInterface { }    struct SampleStruct { }    enum SampleEnum { a, b }    delegate void SampleDelegate(int i);    namespace Nested    {        class SampleClass2 { }    }}

File scoped namespace declarations enable you to declare that all types in a file are in a single namespace. The following example is similar to the previous example, but uses a file scoped namespace declaration:

using System;namespace SampleFileScopedNamespace;class SampleClass { }interface ISampleInterface { }struct SampleStruct { }enum SampleEnum { a, b }delegate void SampleDelegate(int i);

Using Statements in File Scoped Namespaces

When you usefile-scoped namespaces, the placement ofusing statements affects their scope within the file. File-scoped namespaces lower to the equivalent traditional namespace declaration that ends with a closing bracket at the end of the file. This behavior determines whereusing directives are applied as follows:

  • If theusing statements are placed before the file-scoped namespace declaration, they're treated as being outside of the namespace and are interpreted as fully qualified namespaces.
  • If theusing statements are placed after the file-scoped namespace declaration, they're scoped within the namespace itself.

For example:

// This using is outside the namespace scope, so it applies globallyusing System;namespace SampleNamespace; // File-scoped namespace declaration// This using is inside the namespace scopeusing System.Text;public class SampleClass{    // Class members...}

In the preceding example,System is globally accessible, whileSystem.Text applies only withinSampleNamespace.

The preceding example doesn't include a nested namespace. File scoped namespaces can't include more namespace declarations. You can't declare a nested namespace or a second file-scoped namespace:

namespace SampleNamespace;class AnotherSampleClass{    public void AnotherSampleMethod()    {        System.Console.WriteLine(            "SampleMethod inside SampleNamespace");    }}namespace AnotherNamespace; // Not allowed!namespace ANestedNamespace // Not allowed!{   // declarations...}

Within a namespace, you can declare zero or more of the following types:

The compiler adds a default namespace. This unnamed namespace, sometimes referred to as the global namespace, is present in every file. It contains declarations not included in a declared namespace. Any identifier in the global namespace is available for use in a named namespace.

Namespaces implicitly have public access. For a discussion of the access modifiers you can assign to elements in a namespace, seeAccess Modifiers.

It's possible to define a namespace in two or more declarations. For example, the following example defines two classes as part of theMyCompany namespace:

namespace MyCompany.Proj1{    class MyClass    {    }}namespace MyCompany.Proj1{    class MyClass1    {    }}

The following example shows how to call a static method in a nested namespace.

namespace SomeNameSpace{    public class MyClass    {        static void Main()        {            Nested.NestedNameSpaceClass.SayHello();        }    }    // a nested namespace    namespace Nested    {        public class NestedNameSpaceClass        {            public static void SayHello()            {                Console.WriteLine("Hello");            }        }    }}// Output: Hello

C# language specification

For more information, see theNamespaces section of theC# language specification.For more information on file scoped namespace declarations, see thefeature specification.

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo