C#全局命名空间别名(类TestClass:Global::TestApp)
我正在看这一页 他们有下面的代码C#全局命名空间别名(类TestClass:Global::TestApp),c#,C#,我正在看这一页 他们有下面的代码 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;
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()
{
// The following line causes an error. It accesses TestApp.Console,
// which is a constant.
//Console.WriteLine(number);
}
}
他们给出了进一步的例子
我了解这里如何使用global
:
// OK
global::System.Console.WriteLine(number);
但是,我不明白以下内容是什么(尤其是如何在同一行中使用global::TestApp
和:
):
MSDN页面说明了上述代码:“以下声明引用TestApp作为全局空间的成员。”
有人能解释一下吗
谢谢。这是存在于全局级别的类TestApp的强命名,类似于System。如果您说
classtestclass:global::System.Console
,您将继承全局系统控制台(如果这是合法的)。因此,在本例中,您继承了在全局范围内定义的TestApp
因此,为了进一步的清晰,请考虑下面的命名空间模型:
namespace global
{
// all things are within this namespace, and therefor
// it is typically deduced by the compiler. only during
// name collisions does it require being explicity
// strong named
public class TestApp
{
}
namespace Program1
{
public class TestClass : global::TestApp
{
// notice how this relates to the outermost namespace 'global'
// as if it were a traditional namespace.
// the reason this seems strange is because we traditionally
// develop at a more inner level namespace, such as Program1.
}
}
}
这是存在于全局级别的类TestApp的强命名,类似于System。如果您说
classtestclass:global::System.Console
,您将继承全局系统控制台(如果这是合法的)。因此,在本例中,您继承了在全局范围内定义的TestApp
因此,为了进一步的清晰,请考虑下面的命名空间模型:
namespace global
{
// all things are within this namespace, and therefor
// it is typically deduced by the compiler. only during
// name collisions does it require being explicity
// strong named
public class TestApp
{
}
namespace Program1
{
public class TestClass : global::TestApp
{
// notice how this relates to the outermost namespace 'global'
// as if it were a traditional namespace.
// the reason this seems strange is because we traditionally
// develop at a more inner level namespace, such as Program1.
}
}
}
全局
在以下两种情况下的使用方式相同:
global::System.Console.WriteLine(number);
是
System.Console.WriteLine(number);
class TestClass : TestApp
作为
是
System.Console.WriteLine(number);
class TestClass : TestApp
单冒号只是常规继承 全局在以下两种情况下的使用方式相同:
global::System.Console.WriteLine(number);
是
System.Console.WriteLine(number);
class TestClass : TestApp
作为
是
System.Console.WriteLine(number);
class TestClass : TestApp
单冒号只是常规继承 也许这个例子能更好地说明这一点: 代码:
namespace Test
{
class Program
{
static void Main(string[] args)
{
var myClass1 = new MyClass();
var myClass2 = new global::MyClass();
}
public class MyClass { }
}
}
public class MyClass { }
说明:
namespace Test
{
class Program
{
static void Main(string[] args)
{
var myClass1 = new MyClass();
var myClass2 = new global::MyClass();
}
public class MyClass { }
}
}
public class MyClass { }
myClass1
是Test
命名空间中类的实例
myClass2
是global
命名空间中类的一个实例,也称为no命名空间
global::
可用于访问由本地定义的对象隐藏的项目。在这种情况下,Test.MyClass
隐藏了对global::MyClass
的访问,也许这个示例可以更好地说明这一点:
代码:
namespace Test
{
class Program
{
static void Main(string[] args)
{
var myClass1 = new MyClass();
var myClass2 = new global::MyClass();
}
public class MyClass { }
}
}
public class MyClass { }
说明:
namespace Test
{
class Program
{
static void Main(string[] args)
{
var myClass1 = new MyClass();
var myClass2 = new global::MyClass();
}
public class MyClass { }
}
}
public class MyClass { }
myClass1
是Test
命名空间中类的实例
myClass2
是global
命名空间中类的一个实例,也称为no命名空间
global::
可用于访问由本地定义的对象隐藏的项目。在本例中,Test.MyClass
隐藏对global::MyClass
的访问