I have a project with a namespace and class defined like
namespace My.Company.Project.Models
{
class Item
{...}
}
and then in another project I try to use this class:
using My.Company.Project;
...
var obj = new Models.Item
but the compiler says that the "Using directive is unnecessary" and I get an error
CS0246 - The type or namespace name 'Models' could not be found (are you missing a using directive or an assembly reference?)
If my usage instead is
using My.Company;
...
var obj = new Project.Models.Item
it works fine. Why can I use My.Company
but not My.Company.Project
in my using statement and how can I qualify this so that I don't need such verbose namespace naming of classes?
and then in another project I try to use this class:
And highly likely that this project is in My.Company
namespace too. And according to the specification
The following
namespace N1.N2
{
// ...
}
is equivalent to
namespace N1
{
namespace N2
{
// ...
}
}
And (I guess):
Namespaces are open-ended, and two namespace declarations with the same fully qualified name (§7.8.2) contribute to the same declaration space (§7.3).
So both parts will be in the same My.Company
declaration space making using My.Company;
actually obsolete (i.e. you should be able to do var obj = new Project.Models.Item();
even without using, at least this is the way I was able to repro the behavior).
If needed you can define an alias for the Models
:
using Models = My.Company.Project.Models;
//...
var obj = new Models.Item();