使用运算符“::”的地方在OOP C#? [英] Where uses operator "::" in OOP C#?

查看:83
本文介绍了使用运算符“::”的地方在OOP C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个代码中,我看到了带双点的构造::

哪里有用?你能举个例子吗?



谢谢。我在这里学到了很多答案!

In one code i saw construction with double point ::
Where is uses? Can you give me example.

Thank you. I learn much here with your answers!

推荐答案

我们没有秘密或技术可以与你分享,这只是语法的一部分。



请参阅:

http:// www .cplusplus.com / doc / tutorial / namespaces / [ ^ ],

http:// www。 learncpp.com/cpp-tutorial/811-static-member-variables/ [ ^ ]。



您可以在这些手册页上找到足够多的示例。



你甚至不应该在你没有读过它的情况下进行编程。你不应该浪费时间去问这些问题。想象一下,如果每个人都开始询问使用'';'',使用,'' - >''以及其他类似的事情会发生什么?这只是垃圾邮件。如果您开始阅读手册并进行简单的练习,您将学习更容易学习的内容。请这样做,在你至少做一次通行证之前不要再问任何问题。上面引用的网站已经足够好了。



-SA
There are no secrets or techniques we could share with you, this is just the part of syntax.

Please see:
http://www.cplusplus.com/doc/tutorial/namespaces/[^],
http://www.learncpp.com/cpp-tutorial/811-static-member-variables/[^].

You will find more than enough examples on those manual pages.

You should not even get to programming in you did not read about it. And you should not waste time on asking such questions. Imagine what can happen if everyone starts asking about using '';'', "using", ''->'' and other things like that? This is just spam. You will learn what you need to learn much easier if you start reading the manual doing simple exercises as you go. Please do it and refrain from asking any questions until you do at least one pass. The sites referenced above are good enough.

—SA


没有这样的运算符是C#。另一方面,在C ++中......你已经获得了学习它的资源。
There''s no such operator is C#. In C++ on the other hand... You''ve already been given the resources to learn about it.


如果两个程序集对一个类型使用完全相同的名称,那么很明显除非编译器可以唯一地识别每个类型,否则将无法使用这两种类型。外部别名用于解析类型名称歧义,::是命名空间别名和命名空间成员的分隔符。



作为一个疯狂的例子,创建一个名为OtherSystem的程序集包含一种名为System.String的类型。
If two assemblies use exactly the same name for a type it should be obvious that it is going to impossible to use both types unless the compiler can uniquely identify each one. External aliases are used to resolve type name abiguities and :: is the separator for a namespace alias and the namespace member.

As a crazy example create an assembly named OtherSystem containing a type called System.String.
namespace System {
  // Clashes with the built in System.String type
  public class String {
  }
}





任何尝试使用此类型几乎肯定会导致编译失败,并且消息如



Any attempt to use this type will almost certainly result in compilation failure and a message such as

The type 'System.String' exists in both 'OtherSystem.dll' and 'mscorlib.dll'





要解决此问题,当另一个项目引用OtherSystem程序集时,必须为其提供外部别名。



外部别名在C#编译器命令行中定义



To fix this problem the OtherSystem assembly must be given an external alias when it is referenced by another project.

The external alias is defined on the C# compiler command line

csc /reference:MySystem=OtherSystem.dll ExternalAliasTest.cs



但也可以在参考属性表的Visual Studio中设置for OtherSystem.dll。



希望引用别名的源代码必须具有匹配的外部别名指令,以确保正确解析任何带有MySystem ::前缀的类型名称。




but may also be set within Visual Studio on the reference property sheet for OtherSystem.dll.

Source code wishing to refer to the alias must have a matching external alias directive to ensure that any type names prefixed with MySystem:: are resolved properly.

// allow this code to use the external alias
extern alias MySystem;
using System;

namespace ExternalAliasTest {
  class Program {
    static void Main() {
      Console.WriteLine(typeof(System.String).AssemblyQualifiedName);
      Console.WriteLine(typeof(MySystem::System.String).AssemblyQualifiedName);
      Console.ReadLine();
    }
  }
}





程序输出显示我们可以访问两个System.String类型。



The output of the program shows that we are able to access both System.String types.

System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.String, OtherSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null





Alan。



Alan.


这篇关于使用运算符“::”的地方在OOP C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆