> For the complete documentation index, see [llms.txt](https://brianwu.gitbook.io/brian/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://brianwu.gitbook.io/brian/brians-za-ji/za-ji/namespace.md).

# namespace

參考資料:(<https://docs.microsoft.com/zh-tw/dotnet/csharp/programming-guide/namespaces/using-namespaces>)

C# or .NET Framework 利用命名空間來組織多種類別。 宣告自己的命名類別，有助於在較大型的程式設計專案，控制類別和方法名稱的範圍。

## 存取命名空間

程式的開頭先設定好，應該就類似java的import。

```
using System;
```

在程式開始撰寫後，可以這樣寫:

```
Console.WriteLine("Hello world!");
```

就不需要寫成

```
System.Console.WriteLine("Hello world!");
```

## 命名空間別名

```
using Co = Company.Proj.Nested;
```

## 使用命名空間控制範圍

namespace 是用來宣告命名空間的。

從下列程式可觀察出，SampleClass此類別分別在命名空間SampleNamespace及其巢狀命名空間NestedNamespace中。根據底下Program的程式，可以了解是如何呼叫的。

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

    // Create a nested namespace, and define another class.
    namespace NestedNamespace
    {
        class SampleClass
        {
            public void SampleMethod()
            {
                System.Console.WriteLine(
                  "SampleMethod inside NestedNamespace");
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Displays "SampleMethod inside SampleNamespace."
            SampleClass outer = new SampleClass();
            outer.SampleMethod();

            // Displays "SampleMethod inside SampleNamespace."
            SampleNamespace.SampleClass outer2 = new SampleNamespace.SampleClass();
            outer2.SampleMethod();

            // Displays "SampleMethod inside NestedNamespace."
            NestedNamespace.SampleClass inner = new NestedNamespace.SampleClass();
            inner.SampleMethod();
        }
    }
}
```

若有相同名稱

`.`會先找近的，遠的會被隱藏。 `::`會去找命名空間的(別名)。可以使用別名。 `global::`會去找全域命名空間的。不可以使用別名。

(文章內的敘述:在一般情況下，使用 :: 來參考命名空間別名，或使用 global:: 參考全域命名空間，以及使用 . 來限定類型或成員。)

`global`不是預先定義別名，只有在與`::`搭配使用才有特殊意義。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://brianwu.gitbook.io/brian/brians-za-ji/za-ji/namespace.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
