What is global::?

前端 未结 4 1749
盖世英雄少女心
盖世英雄少女心 2020-12-02 16:15

In C# I see global:: used quite often in auto-generated code. It is not something I have ever used myself so I don\'t know what the purpose is. Can someone ex

4条回答
  •  天涯浪人
    2020-12-02 16:48

    It's a sometime-necessary prefix indicating the root namespace.

    It's often added to generated code to avoid name clashes with user code.

    For example, imagine you had a class called System, but then you wanted to use System.String. You could use global::System.String to differentiate.

    I believe the :: comes from C++ where it's used as a namespace separator.

    In practice I've never used it, other than in generating code. Note that you can also get around some conflicts via using aliases. For example using String = System.String;

提交回复
热议问题