Why/when should you use nested classes in .net? Or shouldn't you?

In Kathleen Dollard's 2008 blog post, she offers an intriguing factor to make use of nested courses in.net. Nonetheless, she additionally states that FxCop does not such as nested courses. I'm thinking that individuals creating FxCop regulations aren't foolish, so there have to be thinking behind that placement, yet I have not had the ability to locate it.

102
2022-06-08 08:04:11
Source Share
Answers: 4

It relies on the use. I hardly ever would ever before make use of a Public nested class yet usage Private nested courses every one of the moment. An exclusive nested class can be made use of for a below - object that is planned to be made use of just inside the moms and dad. An instance of this would certainly be if a HashTable class has an exclusive Entry challenge store information inside just.

If the class is suggested to be made use of by the customer (on the surface), I usually like making it a different standalone class.

5
2022-06-08 08:11:43
Source

If I recognize Katheleen is write-up right, she recommends to make use of nested class to be able to write SomeEntity.Collection as opposed to EntityCollection< SomeEntity>. In my point of view it is debatable means to conserve you some keying. I'm rather certain that in real life application collections will certainly have some distinction in executions, so you will certainly require to create different class anyhow. I assume that making use of class name to restrict various other class extent is not an excellent suggestion. It contaminates intellisense and also reinforce dependences in between courses. Making use of namespaces is a typical means to regulate courses extent. Nonetheless I locate that use of nested courses like in @hazzen comment serves unless you have lots of nested courses which suggests negative layout.

3
2022-06-08 08:11:43
Source

From Sun's Java Tutorial:

Why Use Nested Classes? There are numerous engaging factors for making use of nested courses, amongst them:

  • It is a means of practically organizing courses that are just made use of in one area
  • It raises encapsulation
  • Nested courses can bring about even more legible and also maintainable code

Logical group of courses bdsh If a class serves to just one various other class, after that it is sensible to install it because class and also maintain both with each other. Nesting such "helper classes" makes their plan extra structured.

Raised encapsulation bdsh Consider 2 leading - degree courses, An and also B, where B requires accessibility to participants of A that would certainly or else be proclaimed exclusive. By concealing class B within class A, A is participants can be proclaimed exclusive and also B can access them. On top of that, B itself can be concealed from the outdoors. < - This does not relate to C#'s execution of nested courses, this just relates to Java.

Extra legible, maintainable code bdsh Nesting tiny courses within leading - degree courses positions the code more detailed to where it is made use of.

19
2022-06-08 08:10:48
Source

Use a nested class when the class you are nesting is just valuable to the confining class. As an example, nested courses permit you to write something like (streamlined):

public class SortedMap {
    private class TreeNode {
        TreeNode left;
        TreeNode right;
    }
}

You can make a full definition of your class in one area, you do not need to leap via any kind of PIMPL hoops to specify just how your class functions, and also the outdoors does not require to see anything of your execution.

If the TreeNode class was exterior, you would certainly either need to make all the areas public or make a number of get/set approaches to utilize it. The outdoors would certainly have an additional class contaminating their intellisense.

105
2022-06-08 08:10:39
Source