4

Would like to know when it is right to uses a nested classes in C#? Do we have incidents in which the use of it is unjustified and therefore not correct?

If you can give examples for both situations Thanks

Mulder
  • 85
  • 2
  • 4

6 Answers6

8

I find it's convenient to use a nested class when you need to encapsulate a format of data that is primarily going to be used within the parent class. This is usually because the purpose or format of the data is so bespoke to the parent class that it's not really suitable for wider use within your solution.

Brian Scott
  • 9,221
  • 6
  • 47
  • 68
2

Here's a simple basic introduction to nested classes.

Nested_Classes

MattC
  • 3,984
  • 1
  • 33
  • 49
1

C# doesn't have a way to write a using directive to target a class, so that the static members of the class can be accessed without writing the class name as a qualifier (compare with Java's import static, which does allow that).

So for users of your classes, it is a little more convenient if you make any public classes as direct members of a namespace, not nested within other public classes. That way they can pull them into the global namespace with a using directive.

For private classes, go nuts, preferably put them close to where they are used to enhance the readability of your code.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
0

I am not sure if there is room in my world for nested classes. It simply blurs the design for me. If you need to hide the information inside a class, why not just store it in member variables?

Besides, testing becomes more cumbersome without the ability to inject a stub in the place of the class.

Morten
  • 3,778
  • 2
  • 25
  • 45
  • because it's not always just the data you need to encapsulate otherwise structs would be suffice. If you have a class of car and the nested class is engine, the engine needs to have functions such as "Start()". – Brian Scott Mar 23 '11 at 09:43
  • @Brian, But why would you not consider Engine a normal independant class (which also happened to be a property of a Car)? I may just be plain stupid, but I never experienced a need for a nested class. I usually get by with either Engine as a property or letting Car implement the IEngine interface. I get that there may be cases where information hiding are essential, but I haven't experienced those cases personally :-) – Morten Mar 23 '11 at 09:53
  • sure, the case for engine being inner really depends on the scope / context of your application and any implementation of a nested class is arguably a personal preference. – Brian Scott Mar 23 '11 at 10:05
0

User of Nested class is depending upon the scenario like below.

1) Organizing code into real world situations where there is a special relationship between two objects. 2) Hiding a class within another class so that you do not want the inner class to be used from outside of the class it is created within.

Suppose you have 2 classes called A and B and class B is depending upon class A without class A you cannot use class B @ that scenario you can use nested classes

As per my knowledge

DataRow class is nested class for DataTable i.e you cannot create a DataRow Class untill u declare a object of DataTable class

Nitish Katare
  • 1,147
  • 2
  • 12
  • 22
0

I find two main resons:

  1. Personalize a class' name without ruining it.
    Example: Vercas.ExplorerView, where I personalize the name of my class without ruining the meaning.

  2. Private classes.
    Example: Vercas.ExplorerView.Item is used only inside Vercas.ExplorerView.

Vercas
  • 8,931
  • 15
  • 66
  • 106