Scope-resolution operator :: versus member-access operator . in C#

前端 未结 2 638
忘了有多久
忘了有多久 2021-01-18 02:58

In C#, what\'s the difference between A::B and A.B? The only difference I\'ve noticed is that only :: can be used with global

2条回答
  •  梦毁少年i
    2021-01-18 03:48

    the :: operator only works with aliases global is a special system provided alias.

    so ... this works:

    using Foo = System.ComponentModel;
    
    public MyClass {
    
      private Foo::SomeClassFromSystemComponentModel X;
    
    }
    

    but not this:

    public MyClass {
    
      private System.ComponentModel::SomeClassFromSystemComponentModel X;
    
    }
    

    This lets you escape from the hell of sub namespaces that can come about when you are integrating with a library where they have:

    namespace MyAwesomeProduct.System
    {
    
    }
    

    And you in you code have

    using MyAwesomeProduct;
    

    global:: lets you find the real System.

    MSDN info here

提交回复
热议问题