0

I have a directory tree as such:

+Enums
|+MyEnums.cs
+Src
 +Model
  +MyModel.cs

And I would like to use the namespace Enums that I declared in MyEnums.cs in the file MyModel.cs. But I cannot figure out how to do such, because when using the code

using Enums;

I constantly get the error/warning that

The type or namespace name 'Enums' could not be found (are you missing a using directive or an assembly reference?)

If I use it in the same folder I have no issue, but I would like to use it across a larger directory tree.

1
  • There is no relationship between directory structure and namespaces in C#. Commented Dec 19, 2017 at 23:03

2 Answers 2

1

The directory structure of your project and the structure of the namespaces are technically two unrelated things; nevertheless it makes sense to have them organized in parallel, but still they are orthogonal concepts. You have to define a namespace by putting types into it as follows.

namespace Enums
{
    // some definitions
}

namespace Enums.MyEnums
{
    // some more definitions
}

After populating the namespace, you can import the namesepace with the using directive.

Sign up to request clarification or add additional context in comments.

Comments

0

Adding a file to a directory like that typically gives you a namespace that is the same as the directory name, but that is not necessarily the case (such as when you move a file from one folder to another). The namespace is determined like this in the file. Check what the namespace is and change it to Enums if that's what you want it to be:

using System;

namespace Enums
{
    enum Testing
    {
        Test1,
        Test2
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.