共用方式為


. 運算子 (C# 參考)

點運算子 (.) 可用於成員存取。 點運算子指定型別成員或命名空間成員。 例如,使用點運算子來存取 .NET Framework 類別庫內的特定方法:

// The class Console in namespace System:
System.Console.WriteLine("hello");

例如,請參考下列類別:

class Simple
{
    public int a;
    public void b()
    {
    }
}
Simple s = new Simple();

變數 s 擁有 a 和 b 兩個成員,若要存取它們,請使用點運算子:

s.a = 6;   // assign to field a;
s.b();     // invoke member function b;

也可用點組成限定名稱 (Qualified Name),這些名稱指定了它們所屬的 (例如) 命名空間或介面。

// The class Console in namespace System:
System.Console.WriteLine("hello");

使用 using 這個指示詞讓某些限定名稱可有可無:

namespace ExampleNS
{
    using System;
    class C
    {
        void M()
        {
            System.Console.WriteLine("hello");
            Console.WriteLine("hello");   // Same as previous line.
        }
    }
}

但若有模稜兩可的識別項時,就必須指定清楚:

namespace Example2
{
    class Console
    {
        public static void WriteLine(string s){}
    }
}
namespace Example1
{
    using System;
    using Example2;
    class C
    {
        void M()
        {                
            // Console.WriteLine("hello");   // Compiler error. Ambiguous reference.
            System.Console.WriteLine("hello"); //OK
            Example2.Console.WriteLine("hello"); //OK
        }
    }
}

C# 語言規格

如需詳細資訊,請參閱<C# 語言規格>。語言規格是 C# 語法及用法的限定來源。

請參閱

參考

C# 運算子

概念

C# 程式設計手冊

其他資源

C# 參考