1

Sometimes I found someone's codes having nested classes.

When should I create a nested class? Does it make instantiation of the outer class consume more resources?

Thank you in advance.

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
  • 2
    @xport, try the textbox in the upper right corner of the screen. It helps. The one on http://google.com also helps. – Darin Dimitrov Jul 29 '10 at 11:17
  • OK. I am sorry. Thank you any way. – Second Person Shooter Jul 29 '10 at 11:21
  • What "resources" do you care about? – Eric Lippert Jul 29 '10 at 13:28
  • I means the memory occupied to instantiate the outer class object. In my mental model, the nested class has effect on the outer class. – Second Person Shooter Jul 29 '10 at 13:50
  • 1
    OK, *why* does your mental model lead you to believe that a nested type changes the memory size of an instance of the outer type? Suppose you have class B { int x; } class C { int x; class D {} }. Your mental model is that an instance of class C is larger in memory than an instance of class B? Why do you believe that to be true? How much bigger is a C than a B, and what is being stored in the extra space? – Eric Lippert Jul 29 '10 at 15:17
  • 1
    To be clear: either your mental model is wrong or there is some assumption you are making that you're not telling us. I'm asking these questions because it is a big help to me to determine *why people's mental models are wrong*. That helps us design languages, tools, documentation and educational materials that prevent these sorts of misunderstandings. – Eric Lippert Jul 29 '10 at 15:19
  • Thank you for your response. I assume that the existance of D inside C will consume some extra spaces in memory to hold information about this existance. The information may be in the form of metadata, etc. – Second Person Shooter Jul 29 '10 at 16:04
  • @Eric Lippert - When I first encountered nested classes many years ago, I had a vague sort of feeling that the enclosing class should in some way have one of the nested class. Even stronger was the feeling that the nested class should be able to reference the members of a "parent" instance without any sort of qualification. The idiom of passing an instance of the outer class to the inner class's constructor seemed strange and unnecessary. Learning that they were really two different classes and that any relationship between them must be defined explicity was a breakthrough in my C# education. – Jeffrey L Whitledge Jul 29 '10 at 16:13
  • @xport: OK, we're honing in on it. Is your belief that there is extra *space on disk* consumed by the metadata that the compiler must emit to describe the relationship between the classes? That is true; there are an extra couple dozen bytes emitted for that. But that's disk space, not memory. Is your belief that somehow this metadata cost translates into a *per instance of the object created in memory* cost? If so, where does that belief come from? – Eric Lippert Jul 29 '10 at 20:48
  • @Jeffrey: the latter idea - that an instance of the nested class should be coupled to an instance of its outer class - is a reasonable intuition. That's how nested classes work in JScript.NET and in Java. But the extra cost of maintaining a ref to an instance of the outer type is born by each instance of the *inner* type, not by each instance of the *outer* type. – Eric Lippert Jul 29 '10 at 20:50
  • @Eric Lippert - Indeed. But if we suppose a hypothetical language in which each instance of the outer class *automatically* contained exactly one instance of the inner class, then the cost for that instance could conceivably be bourn (completely?) by the outer class. And if we have a second hypothetical language in which each outer class *automatically maintains a collection* of inner class objects, then the outer class must bear some of that burden as well. (Of couse, in this second hypothetical—and some versions of the first—each inner class instance would also have to maintain a reference.) – Jeffrey L Whitledge Jul 29 '10 at 22:55

3 Answers3

2

I think I can see what you're asking.

Suppose you have class A. In Scenerio One this class has no nested class. In Scenerio Two this class has a nested class A.B.

All other things being equal*, the class A will consume no more resources in Scenrio Two than it did in Scenerio One. This is because the two classes A and A.B really are two different classes.

The nesting of classes affects only the scoping of the classes and their members. It does not affect the memory layout of the classes.

*If in Scenerio Two A also contains an instance of an A.B, then not all things would be equal, and class A would be potentially larger than in Scenerio One. But A's member field of type A.B would have to be defined separately from the class definition, so this is a pretty obvious difference.

Does that answer your question?

Jeffrey L Whitledge
  • 58,241
  • 9
  • 71
  • 99
1

You can find some good answers here. Use a nested class when it is only used by the enclosing class.

Community
  • 1
  • 1
raffel
  • 1,385
  • 10
  • 8
  • OK. I have read the link. However, I have not found whether or not instantiating the outer class consume more resources. – Second Person Shooter Jul 29 '10 at 11:30
  • No it doesn't, a nested class behaves just like a top-level class in that aspect. – raffel Jul 29 '10 at 11:48
  • It might help to clarify what you mean by "used". A lot of code uses `List.Enumerator`, for example (using that type rather than `IEnumerator` improves the efficiency of calling `foreach` on a list) – supercat Sep 02 '13 at 19:30
0

When should I create a nested class?

It depends.

Does it make instantiation of the outer class consume more resources?

It depends.

this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137