问题描述
在,她提出了一个有趣的理由在.net中使用嵌套类。但是,她还提到FxCop不喜欢嵌套类。我假设编写FxCop规则的人不是愚蠢的,所以必须在这个位置后面进行推理,但是我还没有找到它。
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.
推荐答案
当嵌套的类只对封闭类有用时使用嵌套类。例如,嵌套类允许你写(简化):
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):
public class SortedMap {
private class TreeNode {
TreeNode left;
TreeNode right;
}
}
您可以在一个地方,你不必跳过任何PIMPL圈定义你的类如何工作,外面的世界不需要看到你的实现任何东西。
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.
如果TreeNode类是外部的,你需要使所有的字段 public
或者做一堆 get / set
使用它的方法。外面的世界会有另一个阶级污染他们的intellisense。
If the TreeNode class was external, you would either have to make all the fields public
or make a bunch of get/set
methods to use it. The outside world would have another class polluting their intellisense.
这篇关于为什么/什么时候应该在.net中使用嵌套类?还是不应该吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!