在 C# 中我看到global::
在自动生成的代码中经常使用。这不是我自己用过的东西,所以我不知道它的目的是什么。有人可以解释一下吗?
global 指的是全局命名空间,它可以用来解决重新定义类型的问题。例如:
class foo
{
class System
{
}
}
如果您要使用 System,它将在 foo 类中本地作用域,您可以使用:
global::System.Console.WriteLine("foobar");
访问全局命名空间。
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
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)