related classes in one file

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,

i have 3 related classes that i think should exist in one file. 2 of the
classes can almost be structs that are used by the main class. my question is
should these 2 classes be nested classes then?

thanks,
rodchar
 
hey all,

i have 3 related classes that i think should exist in one file. 2 of the
classes can almost be structs that are used by the main class. my question is
should these 2 classes be nested classes then?

thanks,
rodchar

Depends, Can those classes used without the first one? In this case
they should not be nested.
In general you declare a nested class when the life of the internal
depends of the external. In general these classes are not creatable
(you cannot create them but by using a method of the outer class)

But it's a design decision really. there are examples of both ways in
the framework.
 
hey all,

i have 3 related classes that i think should exist in one file. 2 of the
classes can almost be structs that are used by the main class. my question is
should these 2 classes be nested classes then?

thanks,
rodchar

Depends, Can those classes used without the first one? In this case
they should not be nested.
In general you declare a nested class when the life of the internal
depends of the external. In general these classes are not creatable
(you cannot create them but by using a method of the outer class)

But it's a design decision really. there are examples of both ways in
the framework.
 
thanks for the help,
rod.

Ignacio Machin ( .NET/ C# MVP ) said:
Depends, Can those classes used without the first one? In this case
they should not be nested.
In general you declare a nested class when the life of the internal
depends of the external. In general these classes are not creatable
(you cannot create them but by using a method of the outer class)

But it's a design decision really. there are examples of both ways in
the framework.
 
thanks for the help,
rod.

Ignacio Machin ( .NET/ C# MVP ) said:
Depends, Can those classes used without the first one? In this case
they should not be nested.
In general you declare a nested class when the life of the internal
depends of the external. In general these classes are not creatable
(you cannot create them but by using a method of the outer class)

But it's a design decision really. there are examples of both ways in
the framework.
 
i have 3 related classes that i think should exist in one file. 2 of the
classes can almost be structs that are used by the main class. my question
is
should these 2 classes be nested classes then?

If they're being privately used by the outer (containing) class then almost
certainly. It promotes encapsulation. If the outside world needs access to
them however then you should still encapsulate them if the inner classes
have no independent use outside of the outer class (typically because they
represent a property of the outer class).
 
i have 3 related classes that i think should exist in one file. 2 of the
classes can almost be structs that are used by the main class. my question
is
should these 2 classes be nested classes then?

If they're being privately used by the outer (containing) class then almost
certainly. It promotes encapsulation. If the outside world needs access to
them however then you should still encapsulate them if the inner classes
have no independent use outside of the outer class (typically because they
represent a property of the outer class).
 
Back
Top