0

Does declaring a nested class has some functional/hierarchical impact on the objects or is just a matter of access/visibility.

I have 4 classes on my program but I only want to make 1 of them public, originally I declared them as follows:

namespace mynamespace{

     private class A{
       // some members;
     }

     private class B{
       // some members;
     }

     private class C{
       // some members;
     }

     public class D{
       // some members;
     }

}

but the compiler complained, so among other things, I changed private to internal, however opposite to what I thought, all 4 classes are available to external programs, which I don't want. I tried everything I could imagine but no luck, so I thought I should try nesting class A, B and C inside class D, as follows:

namespace mynamespace{

     public class D{
       // some members;

        private class A{
           // some members;
        }

        private class B{
           // some members;
        }

        private class C{
          // some members;
        }
     }

}

...this way only the class I want to publish is actually published, however since I'm new to C# I'm not sure this is the proper way to do it. I worry this change could have consequences related to memory consumption, performance, etc.

6
  • 4
    all 4 classes are available to external programs. That's not possible. internal member is only visible within the same assembly. Commented Feb 15, 2014 at 1:18
  • Well, if I declare class A as internal I can access it from any other file on my project. I'm using MonoDelevelop... don't have Visual Studio to see what happens there.
    – george b
    Commented Feb 15, 2014 at 1:26
  • 1
    Same Project = Same Assembly
    – eyalsn
    Commented Feb 15, 2014 at 1:27
  • Then, what if I only want them to be visible to the current file?
    – george b
    Commented Feb 15, 2014 at 1:43
  • If by current file you mean current class, then as @JaredPar suggests in his answer, you want a nested private class. Also, there's nothing in your original snippet that would make the compiler complain, unless you were exposing an instance of any of A, B, or C from within D. Commented Feb 15, 2014 at 1:44

1 Answer 1

0

If the only thing you're trying to accomplish is to make only one accessible to other projects then you don't need nested classes. Just mark all of the ones you don't want visible to other projects as internal (or just use no modifier as internal is the default).

If you want to hide them from other classes within the same project then using a nested private class will do the trick. Do note that nested classes gain access to the non-public members of the type in which they are nested. Hence they gain a bit of power while at the same time being less accessible to other types

2
  • Ok, I don't care too much about nested classes gaining access to non-public members. What worries me is that such nesting could have some impact on my instantiated objects.
    – george b
    Commented Feb 15, 2014 at 1:50
  • @georgeb in what way? Other than accessibility nested classes don't have any tangible impact
    – JaredPar
    Commented Feb 15, 2014 at 2:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.