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

前端 未结 2 634
忘了有多久
忘了有多久 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条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-18 03:33

    with :: you can do things like...

     extern alias X;
     extern alias Y;
     class Test
     {
       X::N.A a;
       X::N.B b1;
       Y::N.B b2;
       Y::N.C c;
     }
    

    and there are times when . is ambiguous so :: is needed. here's the example from the C# language spec

    namespace N
    {
       public class A {}
       public class B {}
    }
    namespace N
    {
       using A = System.IO;
       class X
       {
          A.Stream s1;         // Error, A is ambiguous
          A::Stream s2;        // Ok
       }
    }
    

    http://download.microsoft.com/download/0/B/D/0BDA894F-2CCD-4C2C-B5A7-4EB1171962E5/CSharp%20Language%20Specification.htm

提交回复
热议问题