Nested namespaces

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

I\'ve got something like this:

namespace n1
{
    namespace n2
    {
        class foo{}
    }
}
相关标签:
5条回答
  • 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.

    0 讨论(0)
  • 2020-11-30 01:53

    This is a deliberate rule of C#. If you do this:

    namespace Frobozz
    {
        namespace Magic
        {
            class Lamp {}
        }
    
        class Foo
        {
            Magic.Lamp myLamp; // Legal; Magic means Frobozz.Magic when inside Frobozz
        }
    }
    

    That is legal. But this is not:

    namespace Frobozz
    {
        namespace Magic
        {
            class Lamp {}
        }
    }
    
    namespace Flathead
    {
        using Frobozz;
        class Bar
        {
            Magic.Lamp myLamp; // Illegal; merely using Frobozz does not bring Magic into scope
        }
    }
    

    The rule of C# that describes this is in section 7.6.2 of the C# 4 spec. This is a very confusing section; the bit you want is the paragraph near the end that says

    Otherwise, if the namespaces imported by the using-namespace-directives of the namespace declaration contain exactly one type having name I...

    The key point is that it says "exactly one type", not "exactly one type or namespace". We deliberately disallow you "slicing" a namespace name like this when you are outside of that namespace because it is potentially confusing. As others have said, if you want to do that sort of thing, fully qualify it once in a using-alias directive and then use the alias.

    0 讨论(0)
  • 2020-11-30 01:56

    Use namespace aliases:

    using n2 = n1.n2;
    
    ...
    n2.foo something;
    

    What is before the class name should be a complete name space (with/or other class name(s) for nested types). A truncated namespace will not work.

    0 讨论(0)
  • 2020-11-30 02:01

    By design, namespaces are there to help you define scope.

    Unless you fully qualify it, you will get the error you're seeing.

    Assuming File1 has something like this:

    namespace n1
    {
        namespace n2
        {
            class Foo { }
        }
    }
    

    You can do this two ways:

    Fully qualified using

    File2 contents:

    namespace n3
    {
        using n1.n2;
    
        class TestClass
        {
            private Foo something;
        }
    }
    

    Use a namespace alias

    namespace n3
    {
        using n2 = n1.n2;
    
        class TestClass
        {
            private n2.Foo something;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 02:01

    You cannot write this, as n2 is inside of n1. If you want to access the n2 namespace, you can try typing using n2 = n1.n2 at the beginning of your other file.

    0 讨论(0)
提交回复
热议问题