有什么用global::
C# 中的关键字?什么时候我们必须使用这个关键字?
从技术上来说,global
不是关键字:它是所谓的“上下文关键字”。它们仅在有限的程序上下文中具有特殊含义,并且可以在该上下文之外用作标识符。
global
每当有歧义或成员被隐藏时,就可以而且应该使用。从here http://msdn.microsoft.com/en-us/library/c3ay4x3d(VS.80).aspx:
class TestApp
{
// Define a new class called 'System' to cause problems.
public class System { }
// Define a constant called 'Console' to cause more problems.
const int Console = 7;
const int number = 66;
static void Main()
{
// Error Accesses TestApp.Console
Console.WriteLine(number);
// Error either
System.Console.WriteLine(number);
// This, however, is fine
global::System.Console.WriteLine(number);
}
}
但请注意,global
当没有为类型指定命名空间时不起作用:
// See: no namespace here
public static class System
{
public static void Main()
{
// "System" doesn't have a namespace, so this
// will refer to this class!
global::System.Console.WriteLine("Hello, world!");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)