2

I'm finding some frustration with the way C# handles namespaces when I want to reference a class whose class name happens to be identical to part of the current class's namespace. This will produce a compiler error, "'Class' is a namespace but is used like a type".

Consider the following code sample, which demonstrates the problem:

namespace A.B.C
{
    public class Y
    {      
    }
}

namespace X.Y.Z
{
    public class Class1
    {
        public Y MyProp;
    }
}

In this case, I want to use the class "Y" from the namespace "A.B.C". But because "Y" is also one of the parts of the namespace, C# treats "Y" as a namespace, not a type.

I can get around this by fully qualifying the class name, "A.B.C.Y", or using an alias, but my general preference would be for C# not to treat "Y" as a namespace in this context. Often I have code, such as test code, that contains similar namespaces as classes its testing, and this kind of class/namespace collision means having to be a lot more verbose in setting things up.

I'm not sure what the process is called, but it seems that in resolving classes and namespaces, C# will walk up the namespaces until it finds a part of the namespace that matches. In this case, it walks up and finds Y. Is there a way to tell C# not to walk up, not to allow this partial namespace matching?

Dan
  • 1,125
  • 1
  • 13
  • 22
  • Use the fully qualified name in this case, or add a `using` statement for the other namespace, otherwise the compiler doesn't know if you're referencing a namespace or a class. – Rufus L Oct 10 '19 at 16:45
  • 3
    Possible duplicate of [Namespace and class with the same name?](https://stackoverflow.com/questions/18731415/namespace-and-class-with-the-same-name) – Matthew Thurston Oct 10 '19 at 16:46
  • I believe the process is called "namespace resolution" and is [documented in the C# specification](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/namespaces) – Heretic Monkey Oct 10 '19 at 16:47
  • @MattThurston That question is asking how to organise/name namespaces and classes. However, OP seems to be implying that renaming the namespace or the class is not an option. OP, is this true? – Sweeper Oct 10 '19 at 16:48
  • I agree that fully qualifying the namespace of the class I'm using is a reasonable solution, it's just somewhat cumbersome. The surprising aspect of namespace resolution in this case was the seemingly "partial" namespace resolution, where it's matching on a word in the middle of the namespace. The ideal solution would be to have some way to disable this "partial" namespace matching, and require either explicit fully-qualified namespaces. So in this example, I'd prefer that "Y" on its own would not be interpreted as a namespace. If I want that namespace, I'd type "X.Y" and fully qualify it. – Dan Oct 10 '19 at 17:11

1 Answers1

8

If you write using A.B.C; (the namespace that Y is in) inside namespace X.Y.Z, the error goes away:

namespace X.Y.Z
{
    using A.B.C;
    public class Class1
    {
        public Y MyProp;
    }
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 1
    Thanks, this is actually really helpful, and something I didn't know. Time to go look up about using 'using' directives within a namespace, and how that differs from putting the 'using' outside the namespace... – Dan Oct 10 '19 at 17:15