简体   繁体   English

C#Namespace Alias限定符(::) vs解除引用运算符(。)

[英]C# Namespace Alias qualifier (::) vs Dereferencing Operator (.)

Quick and simple question. 快速而简单的问题。 I kind of understand what the Namespace Alias qualifier does, it's for accessing members in a namespace, however so does the dereferencing operator. 我有点理解命名空间别名限定符的作用,它用于访问命名空间中的成员,但解除引用操作符也是如此。 I am really baffled as to the difference in this situation, why you would use one over the other, or how they each accomplish the same thing. 我对这种情况的不同感到困惑,为什么你会使用一个而不是另一个,或者他们各自如何完成同样的事情。

using colAlias = System.Collections;

namespace myns
{
    class TestApp
    {
        static void Main()
        {
            colAlias.Hashtable test = new colAlias.Hashtable();
            colAlias::Hashtable test1 = new colAlias::Hashtable();
        }
    }
}

This is a corner case :: (like the @ prefix) is there to deal with the fairly rare occurrences where a name conflicts between namespaces, classes and keywords. 这是一个极端情况:: :(就像@前缀一样)是为了处理命名空间,类和关键字之间名称冲突的极少数情况。

:: only works for namespaces (and namespace aliases), while . ::仅适用于名称空间(和名称空间别名),而. . works for both namespaces and subclasses. 适用于命名空间和子类。 Most places where you'd need it you'd be better off using a different name instead, but that isn't always an option. 大多数你需要它的地方你最好使用不同的名字,但这并不总是一个选择。

global:: is a special case that's most often seen in auto-generated code - it resets the referenced namespace to the root. global::是自动生成代码中最常见的特殊情况 - 它将引用的命名空间重置为根。

For instance, suppose you auto-generate some code (maybe for a forms app, EF, or similar) and your app uses the namespace YourCompany.Application . 例如,假设您自动生成一些代码(可能用于表单应用程序,EF或类似代码),并且您的应用程序使用名称空间YourCompany.Application Now one of your customers (using your auto-generation) decides to add their own namespace in their app TheirCompany.YourCompany.Application . 现在,您的一个客户(使用您的自动生成)决定在他们的应用程序TheirCompany.YourCompany.Application添加自己的命名空间。 Now all your auto code fails because when it compiles .Net doesn't know whether to use your namespace or theirs. 现在你的所有自动代码都失败了,因为它编译时.Net不知道是否使用你的命名空间或他们的命名空间。

To fix this generate code with global::YourCompany.Application , then those that use your auto-generator can use whatever namespace they like and not conflict. 要使用global::YourCompany.Application修复此生成代码,那么使用您的自动生成器的那些代码可以使用他们喜欢的任何命名空间而不会发生冲突。

I think Microsoft added global:: because they expected some .Net customers to add namespaces like System . 我认为微软增加了global::因为他们希望一些.Net客户添加像System这样的名称空间。

You said: 你说:

Namespace Alias qualifier does, it's for accessing members in a namespace , however so does the dereferencing operator. 命名空间别名限定符用于访问命名空间中的成员,但解除引用操作符也是如此。

Well, no. 好吧,不。 The . . operator is used to access any member, including functions. operator用于访问任何成员,包括函数。 You cannot do Console::WriteLine(); 不能Console::WriteLine();

:: is only for resolving namespaces, either from a namespace alias like this: ::仅用于从名称空间别名解析名称空间,如下所示:

using colAlias = System.Collections;
...
...
colAlias::Hashtable test = new colAlias::Hashtable();

OR from global. 来自全球。

global::System.Console.WriteLine(..);

You cannot do : 可以这样做:

System.Collections::ArrayList a = new System.Collections.ArrayList();

BUT, if you have an alias the . 但是,如果你有一个别名. operator also works, so in your case , there is no difference. 操作员也可以工作,所以在你的情况下 ,没有区别。

There's an MSDN page explaining how this works. 有一个MSDN页面解释了它是如何工作的。

Basically, in your situation they will achieve the same thing and for code readability it's preferred to use a single . 基本上,在您的情况下,他们将实现相同的目标,并且对于代码可读性,首选使用单个. .

I wouldn't use the :: operator on anything but the global namespace, and even then there are more than enough ways to work around it. 我不会在除了全局命名空间之外的任何东西上使用::运算符,即使这样,也有足够的方法来解决它。

edit: More information what the operator does is explained at the :: Operator (C# Reference) article. 编辑:有关运算符操作的更多信息,请参阅::运算符(C#参考)文章。

The general idea of a namespace qualifier is to allow you reference the namespace even if the name has been used elsewhere. 命名空间限定符的一般概念是允许您引用命名空间,即使该名称已在其他地方使用过。 If you declared a class named "colAlias" then colAlias.Hashtable would reference the class but colAlias::Hashtable would reference the namespace'd value. 如果您声明了一个名为“colAlias”的类,则colAlias.Hashtable将引用该类,但colAlias :: Hashtable将引用该命名空间的值。

This is a fairly narrow use-case and global:: is the only typical use case I have seen for this operator (When trying to ensure no conflicts can occur when creating generated code to be compiled in an unknown application). 这是一个相当狭窄的用例, global::是我在这个运算符中看到的唯一典型用例(当尝试确保在创建生成的代码以便在未知应用程序中编译时不会发生冲突)。

The namespace alias qualifier (::) helps you to access namespace methods without causing errors if you have CONFLICTING namespaces using the same naming convention. 如果您使用相同的命名约定具有CONFLICTING命名空间,则命名空间别名限定符(::)可帮助您访问命名空间方法而不会导致错误。

For example as explained here in msdn http://msdn.microsoft.com/en-us/library/c3ay4x3d(v=vs.80).aspx 例如,如msdn http://msdn.microsoft.com/en-us/library/c3ay4x3d(v=vs.80).aspx中所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM