C# 什么是全局::?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15022441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is global::?
提问by Sachin Kainth
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 explain this?
在 C# 中,我global::
经常在自动生成的代码中使用。这不是我自己用过的东西,所以我不知道目的是什么。有人可以解释一下吗?
采纳答案by chrisw
global refers to the global namespace, it can be used to solve problems whereby you may redefine types. For example:
global 指的是全局命名空间,它可用于解决您可以重新定义类型的问题。例如:
class foo
{
class System
{
}
}
If you were to use System where it would be locally scoped in the foo class, you could use:
如果您要在 foo 类中本地范围内使用 System,则可以使用:
global::System.Console.WriteLine("foobar");
to access the global namespace.
访问全局命名空间。
Example
例子
using System;
class Foo
{
public void baz()
{
Console.WriteLine("Foo 1");
}
}
namespace Demo
{
class Foo
{
public void baz()
{
Console.WriteLine("Foo 2");
}
}
class Program
{
protected static global::Foo bar = new global::Foo();
static void Main(string[] args)
{
bar.baz(); // would write Foo 1 to console as it refers to global scope
Foo qux = new Foo();
qux.baz(); // would write Foo 2 to the console as it refers to the Demo namespace
}
}
}
回答by Drew Noakes
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.
例如,假设您有一个名为 的类System
,但随后您想使用System.String
. 你可以global::System.String
用来区分。
I believe the ::
comes from C++ where it's used as a namespace separator.
我相信::
它来自 C++,它被用作命名空间分隔符。
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;
在实践中,除了生成代码之外,我从未使用过它。请注意,您还可以通过使用别名来解决一些冲突。例如using String = System.String;
回答by coder
The global
contextual keyword, when it comes before the :: operator, refers to the global namespace, which is the default namespace for any C# program and is otherwise unnamed.
当global
上下文关键字出现在 :: 运算符之前时,它指的是全局命名空间,它是任何 C# 程序的默认命名空间,否则是未命名的。
The global::
specifier tells the compiler to start looking for the namespace or class starting from the root. You'll see it in system-generated code so that the code always works. That way if you have a namespace right under your current namespace that is the same as the top level namespace the code is trying to access, there won't be a conflict.
该global::
说明符告诉编译器开始寻找从根本上命名空间或类起动。您将在系统生成的代码中看到它,以便代码始终有效。这样,如果当前命名空间下的命名空间与代码尝试访问的顶级命名空间相同,则不会发生冲突。
For example, say you have namespace A and namespace B and namespace B.A if I write code in namespace B.A that needs to reference a class in namespace A, without global:: I have no way of getting to it. If I reference A.classname, the compiler will look for classname in B.A. With global:: I can tell it to look for classname in global::A.classname and it will find classname in the proper location.
例如,假设您有命名空间 A 和命名空间 B 以及命名空间 BA,如果我在命名空间 BA 中编写需要引用命名空间 A 中的类的代码,而没有 global:: 我无法获得它。如果我引用 A.classname,编译器将在 BA 中使用 global:: 查找类名:我可以告诉它在 global::A.classname 中查找类名,它会在适当的位置找到类名。
回答by Stokely
The global::
namespace and its identifier is not what most people think. It is not a universal identifier of everything created in an application that lies outside one of your application's defined namespaces and which is attached to some global root.
该global::
命名空间和其标识是不是大多数人的想法。它不是应用程序中创建的所有内容的通用标识符,这些内容位于应用程序定义的命名空间之一之外并附加到某个全局根。
If you create a class or type outside your top level namespaces you would assume its automatically a part of the GLOBAL namespace and accessible by the global::
identifier in all files in your application or assembly. In fact those names are more often in that file's compiled LOCAL scope only, yet are accessible via the global::
identifier.
如果您在顶级命名空间之外创建一个类或类型,您将自动假定它是 GLOBAL 命名空间的一部分,并且可以通过global::
应用程序或程序集中所有文件中的标识符访问。事实上,这些名称通常仅在该文件的已编译 LOCAL 范围内,但可以通过global::
标识符访问。
If you create a top level class or namespace in an aspx.cs file it is accessible via global::
from the global namespace in that file. But if you type global::
in another file, that class and namespace doesn't exist in the global namespace. If you create that same class or namespace in a class.cs file however, those items are available to all other files via global::
and in the global namespace as well as that files local scope. Why?
如果您在 aspx.cs 文件中创建顶级类或命名空间,则可以通过global::
该文件中的全局命名空间访问它。但是,如果您键入global::
另一个文件,则全局命名空间中不存在该类和命名空间。但是,如果您在 class.cs 文件中创建相同的类或命名空间,则这些项目可通过global::
全局命名空间以及该文件的本地范围用于所有其他文件。为什么?
It turns out global::
is really a reference to top level LOCAL names under the file's scope as well as GLOBAL names shared by the assembly (like what might be compiled in your App_Code class files in a typical ASP.NET project).
事实证明,global::
它实际上是对文件范围内的顶级 LOCAL 名称以及程序集共享的 GLOBAL 名称的引用(例如可能在典型 ASP.NET 项目的 App_Code 类文件中编译的名称)。
I found this very confusing and not consistent, since global::
implies access to top-level namespaces and types created in the application that are tied to the global namespace. Some like "System" are tied to the global namespace by default in all files, but custom ones may or may not be depending on the scope of that file. That is why the global identifier has a secondary role of resolving references to your local root scope names as well.
我发现这非常令人困惑且不一致,因为这global::
意味着访问与全局命名空间相关的应用程序中创建的顶级命名空间和类型。一些像“系统”这样的默认情况下在所有文件中都绑定到全局命名空间,但自定义的可能取决于该文件的范围,也可能不取决于该文件的范围。这就是为什么全局标识符还具有解析对本地根作用域名称的引用的次要作用的原因。
You can test this by creating top level namespaces and classes in parts of your application then using global::
to see which ones it can access in the global namespace from different parts of your application and which ones it cannot. The one's it cannot access are clearly assigned to a "local global scope" in that file only, which global::
helps you access in naming conflicts.
您可以通过在应用程序的各个部分创建顶级命名空间和类,然后使用它global::
来查看它可以从应用程序的不同部分访问全局命名空间中的哪些内容,以及不能访问哪些内容来对此进行测试。它无法访问的那些被明确分配给该文件中的“本地全局范围”,这global::
有助于您在命名冲突中访问。