0

I have a compiled class library with a root namespace Protege.MyLibrary.

It has a few root types, for example, CommonlyNamedType.

When I add the library to my consuming application, I'd like, for clarity in some situations, to be able to specify variables as:

using Protege;
...
MyLibrary.CommonlyNamedType oMyType = new MyLibrary.CommonlyNamedType;

rather than

using Protege.MyLibrary;
...
CommonlyNamedType oMyType = new CommonlyNamedType;

The former doesn't compile, indicating for the namespace Protege "Using directive is unnecessary", and that is can be removed.

This seems bizarre as I could go the other way and add additional namespaces, such as Protege.MyLibrary.AnotherNamespace.

I seem to be able to do this 100% okay in VB.NET - using either or both Imports Protege and/or Protege.MyLibrary and even optionally qualifying types with redundancy. But not in C#.NET.

I have had a good look around SO and other places and haven't seen an explanation for this behavior. Any ideas?

SteveCinq
  • 1,920
  • 1
  • 17
  • 22
  • 1
    Yes, it would make code cleaner in some cases. But it looks, that a namespace can exist on its own only it it has actual types in it. – ZorgoZ Jan 03 '19 at 05:51
  • "Using directive is unnecessary" is [warning](https://stackoverflow.com/questions/48402517/what-makes-a-using-directive-unnecessary) and you can disable it if you really want... But as Eric Lippert pointed out in linked question there is a good chance you actually ending up with different type... It may be good idea to provide [MCVE] (you don't need separate libraries to show that - you can do that with just two files) – Alexei Levenkov Jan 03 '19 at 06:07
  • @ZorgoZ Nice observation. I hadn't thought of that. Weird that it doesn't trouble (lowly, unloved) VB in the least. – SteveCinq Jan 03 '19 at 06:42
  • It's not that there's some insurmountable technical challenge. It's just two different languages with two sets of design goals that lead to different prioritisations. – Damien_The_Unbeliever Jan 03 '19 at 08:28
  • @ZorgoZ Update: I checked this. I made the root namespace just `MyCompany` and added `Namespace = MyLibrary` to all of the code files then added a public `Enum Dummy ...` under the `MyCompany` namespace. Although the assembly shows `Dummy` under `MyCompany`, and I can just import `MyCompany` and use `MyCompany.Dummy`, I still can't use MyLibrary.CommonlyNamedType. So I'm still a bit clueless here. – SteveCinq Jan 03 '19 at 23:58

1 Answers1

0

You can statically import a class like

using static System.IO.File;

It doesn't support for class, you need to use normal using for import the namespace.

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79