C#中的::运算符的作用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WhoisPo/article/details/88621228

这个运算符用来限定命名空间使用别名的优先级高于层级关系中的命名空间。

比如有下面命名空间的定义

using Mynest1= Myroot.Mynest2

namespace Myroot
{
	namespace Mynest1
	{
		public class myclass {}
	}
	namespace Mynest2
	{
		public class myclass {}
	}
}

那么在Myroot命名空间下,如果使用Mynest1.myclass,那么指的就是 Myroot.Mynest1.myclass。

为了能够使用别名所指的命名空间,也就是Myroot.Mynest2.myclass,就需要使用::运算符

Mynest1::myclass

在C#中,有一个代表顶级命名空间的别名 global,比如你想使用

System.Collections.Generic.List<int>

但是你有担心,在该代码所在的命名空间中,还有其他用户写的一个嵌套的 System.Collections.Generic命名空间,导致你所引用的对象不是你想引用空间中的对象。

这个时候,你就可以用这个方法

global::System.Collections.Generic.List<int>

猜你喜欢

转载自blog.csdn.net/WhoisPo/article/details/88621228