Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Toán tử phân giải phạm vi (Scope Resolution Operator) trong C#

Toán tử :: trong C# khác C++. Ở C++ dùng cho global variable, C# liên quan đến namespace.

Nếu type trùng identifier ở namespace khác, dùng :: để phân biệt.

Ví dụ tham chiếu System.Console với global namespace alias:

global::System.Console

Ví dụ

using myAlias = System.Collections;
namespace Program {
    class Demo {
        static void Main() {
            myAlias::Hashtable h = new myAlias::Hashtable();
            h.Add("Q", "1"); h.Add("R", "2"); h.Add("S", "3"); h.Add("T", "4"); h.Add("U", "5");
            foreach (string n in h.Keys) global::System.Console.WriteLine(n + " " + h[n]);
        }
    }
}

Kết quả

T 4 R 2 S 3 U 5 Q 1