Tom
Tom

Reputation: 2230

'Class' is a namespace but is used like a 'type'

First, this is not the same as the many highly upvoted questions on this exact topic unless I'm missing one of them. All of them point that the issue is I have a namespace with the same name as the class. This is not the case (but it was).

I started out creating a new console application called BatchResizer and put a couple of classes there, but then decided to move this into a class library, called BatchResizer.Components; I then renamed the original console application to BatchResizer.ConsoleRunner, changed all classes in that project to namespace BatchResizer.ConsoleRunner.[...], set the assembly name and default namespace to the same.

There is a class titled BatchResizer but there are no namespaces titled [...].BatchResizer in the project anymore, but when I do var batchResizer = new BatchResizer() I get the error that the namespace is used like a class. There are items named like BatchResizer.ConsoleRunner.[...] or BatchResizer.Components.[...], but nothing ending in BatchResizer.

I've tried "cleaning" and rebulding the project, deleting the .suo file, deleting the /bin folder of all projects in the solution, and I've went through every class in all related projects for namespace collisions.

Upvotes: 3

Views: 22622

Answers (2)

Charles Mager
Charles Mager

Reputation: 26223

BatchResizer is still a namespace name, though. If it's also the same name as a class, you'll have to be more explicit:

var batchResizer = new Components.BatchResizer();

You could also add a using statement within your namespace:

namespace BatchResizer.ConsoleRunner
{
    using Components;   

    internal class Program
    {        
        private static void Main(string[] args)
        {
            var batchResizer = new BatchResizer();
        }
    }
}

If you want to get a bit geeky, then the C# 5.0 spec has this to say:

9.2 Namespace declarations

...The qualified-identifier of a namespace-declaration may be a single identifier or a sequence of identifiers separated by “.” tokens. The latter form permits a program to define a nested namespace without lexically nesting several namespace declarations. For example,

namespace N1.N2
{
    class A {}
    class B {}
}

is semantically equivalent to

namespace N1
{
    namespace N2
    {
        class A {}
        class B {}
    }
}

So even if, as you say, no class is declared in the namespace BatchResizer, BatchResizer is declared as a namespace.

Upvotes: 15

Jalkar
Jalkar

Reputation: 71

First, this is not the same as the many highly upvoted questions on this exact topic unless I'm missing one of them. All of them point that the issue is I have a namespace with the same name as the class. This is not the case (but it was).

BatchResizer may not be a 'final' namespace, but it' still a namespace

Namespace : Foo.BatchResizer.Components
            Foo.BatchResizer.ConsoleRunner
Class :     Foo.BatchResizer

Upvotes: 1

Related Questions