48

I have a class library that has a couple of namespaces containing only internal types.

However, when using the class library in an application project, the namespaces shows up in intellisense, but of course they are empty. Is there any way for me to hide the namespaces completely when using intellisense in other projects?

I've tried to apply EditorBrowsableAttribute to all the internal classes as well, but what I'd like to do would be to apply that to the namespace, which is of course impossible.

Or is, if I care enough about this, the only option I have to just move the types into a namespace that contains public types?

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • 2
    This was kind of hard to Google...everything kept coming up for people who wanted to hide a class inside a namespace or assembly. – Panzercrisis Sep 01 '16 at 14:45

2 Answers2

51

It depends on how you're referencing your class library:

  • If you have the class library project contained within your solution and use a project reference, you'll always see that empty namespace via Intellisense.
  • If you're referencing a compiled dll of your class library, you won't see the namespace popping up in intellisense, provided it contains only internal members.

Try this:

namespace ClassLibrary1
{
    namespace Internal
    {
        internal class InternalClass
        {
            public int internalStuff { get; set; }
        }
    }

    namespace Public
    {
        public class PublicClass
        {
            public int publicStuff { get; set; }
        }
    }
}

If you reference this via a project reference, you'll see the empty namespace. If you reference a dll of it, you won't.

takrl
  • 6,356
  • 3
  • 60
  • 69
  • Before reading this answer, I was starting to organize sets of classes and stuff into subfolders while keeping them in the project's root namespace, but that just really didn't feel right. I can definitely live with them only being visible with project references though, so now I'm going to start adding the sub-namespaces in. – Panzercrisis Sep 01 '16 at 14:49
7

I've come up against this before and found no solution, only a workaround which may or may not work in your project. Instead of defining a namespace you could use an nested static class?

James L
  • 16,456
  • 10
  • 53
  • 70
  • Yeah, I could do that, but then I'd need to rewire all references, as nested classes will need to be part of the qualifying name all over the place. ie. I'd need to write this: var x = new NotReallyANamespace.ClassName(); instead of just adding the using directive. +1 for idea though :) – Lasse V. Karlsen Apr 06 '09 at 08:58
  • I know, it's a long way from perfect :( – James L Apr 06 '09 at 09:00
  • 4
    Now that C# has `using static`, the nested static class approach has become somewhat more feasible. – stakx - no longer contributing Jun 14 '16 at 23:52