related classes in one file

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
 
I

Ignacio Machin ( .NET/ C# MVP )

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.
 
I

Ignacio Machin ( .NET/ C# MVP )

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.
 
R

rodchar

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.
 
R

rodchar

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.
 
L

Larry Smith

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).
 
L

Larry Smith

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top