Nested namespaces
I've got something like this:
namespace n1
{
namespace n2
{
class foo{}
}
}
In other file I write:
using n1;
Why I can't type now something like:
n2.foo smthing;
And how to make something like this possibile?
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.
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.
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;
}
}