In Kathleen Dollard's recent blog post, she presents an interesting reason to use nested classes in .net. However, she also mentions that FxCop doesn't like nested classes. I'm assuming that the people writing FxCop rules aren't stupid, so there must be reasoning behind that position, but I haven't been able to find it.
相关问题
- Generic Generics in Managed C++
- What uses more memory in c++? An 2 ints or 2 funct
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
Use a nested class when the class you are nesting is only useful to the enclosing class. For instance, nested classes allow you to write something like (simplified):
You can make a complete definition of your class in one place, you don't have to jump through any PIMPL hoops to define how your class works, and the outside world doesn't need to see anything of your implementation.
If the TreeNode class was external, you would either have to make all the fields
public
or make a bunch ofget/set
methods to use it. The outside world would have another class polluting their intellisense.As nawfal mentioned implementation of Abstract Factory pattern, that code can be axtended to achieve Class Clusters pattern which is based on Abstract Factory pattern.
I like to nest exceptions that are unique to a single class, ie. ones that are never thrown from any other place.
For example:
This helps keep your project files tidy and not full of a hundred stubby little exception classes.
Another use not yet mentioned for nested classes is the segregation of generic types. For example, suppose one wants to have some generic families of static classes that can take methods with various numbers of parameters, along with values for some of those parameters, and generate delegates with fewer parameters. For example, one wishes to have a static method which can take an
Action<string, int, double>
and yield aString<string, int>
which will call the supplied action passing 3.5 as thedouble
; one may also wish to have a static method which can take an anAction<string, int, double>
and yield anAction<string>
, passing7
as theint
and5.3
as thedouble
. Using generic nested classes, one can arrange to have the method invocations be something like:or, because the latter types in each expression can be inferred even though the former ones can't:
Using the nested generic types makes it possible to tell which delegates are applicable to which parts of the overall type description.
yes for this case:
If I understand Katheleen's article right, she proposes to use nested class to be able to write SomeEntity.Collection instead of EntityCollection< SomeEntity>. In my opinion it's controversial way to save you some typing. I'm pretty sure that in real world application collections will have some difference in implementations, so you will need to create separate class anyway. I think that using class name to limit other class scope is not a good idea. It pollutes intellisense and strengthen dependencies between classes. Using namespaces is a standard way to control classes scope. However I find that usage of nested classes like in @hazzen comment is acceptable unless you have tons of nested classes which is a sign of bad design.