Nested namespaces

前端 未结 5 1739
慢半拍i
慢半拍i 2020-11-30 01:08

I\'ve got something like this:

namespace n1
{
    namespace n2
    {
        class foo{}
    }
}
5条回答
  •  萌比男神i
    2020-11-30 01:41

    Section 9.4.2 paragraph 4 in the C# language specification explains this behavior explicitly:

    A using-namespace-directive imports the types contained in the given namespace, but specifically does not import nested namespaces.

    It even goes on to give an example that is very similar to your own.

    namespace N1.N2
    {
        class A {}
    }
    namespace N3
    {
        using N1;
        class B: N2.A {}        // Error, N2 unknown
    }
    

    Of course had you done this:

    namespace n1
    {
      public class Example
      {
        public static void Main()
        {
          n2.Foo a; // This is legal.
        }
      }
    }
    

    This would compile because n2 is accessible since it is referenced from within an ancestor namespace block.

提交回复
热议问题