拥有一个带有私有访问器的公共字段有意义吗?
我有一个名为 GestorePersonale
的类,它包含另一个类的实例列表:
public List<Dipendente> Dipendenti { get; private set; }
我希望仅通过该类公开的方法而不是直接修改该列表。我注意到使用上面的代码,人们可以做
var gp = new GestorePersonale(); gp.Dipendenti.Add( new Dipendente( ... ) );and be able to perform any other kind of action on the
List<Dipendente>
itself.我考虑将第一个代码片段转换为
private List dipendenti;
,但我可以发现一些缺点:
- 这会打破我的个人规则,即尽可能在类的方法内部尝试始终使用公共字段而不是私有字段(即使我'我不确定这样做是否是好的做法,因此欢迎任何澄清);
- 这将削弱任何外部实体仅出于读取目的访问列表内容的能力,例如对列表内容执行 LINQ 查询。
解决这种情况的最佳方法是什么?
I have a class called GestorePersonale
which holds a list of instances of another class:
public List<Dipendente> Dipendenti { get; private set; }
I want to keep this list modifiable only from the methods the class exposes, and not directly. I noticed that with the code above, one could just do
var gp = new GestorePersonale(); gp.Dipendenti.Add( new Dipendente( ... ) );
and be able to perform any other kind of action on the List<Dipendente>
itself.
I considered converting the first code snippet to
private List dipendenti;
but I could find a few downsides to that:
- This would break the personal rule of mine to try to always use the public fields over the private ones from inside the class's methods whenever possible (even though I'm not sure if it is good practice to do so, so any clarification would be welcome);
- This would impair any external entities' ability to access the contents of the list for reading purposes only, like, say, to execute a LINQ query over the contents of the list.
What would be the best way to solve this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将列表包装在 ReadOnlyCollection 中并公开:
在内部,您可以访问对象。包装列表,因此他们只能读取,但不能添加/删除项目。
dipendenti
并可以添加/删除项目。外部实体只能访问 ReadOnlyCollectionYou can wrap the list in a ReadOnlyCollection<T> and expose that:
Internally, you have access to
dipendenti
and can add/remove items. External entities have access only to the ReadOnlyCollection<T> that wraps the list, so they can only read, but not add/remove items.我同意 dtb 的观点,即 ReadOnlyCollections 是可行的方法。但是,您可以从属性 getter(使用 AsReadOnly)返回它并删除该方法。
I would agree with dtb that ReadOnlyCollections is the way to go. However, you can return it from the property getter (using AsReadOnly) and drop the method.
您可以执行以下几项操作:
ReadOnlyCollection
IEnumerable<_type>
您使用的方法取决于您需要的功能以及您想要/需要向类的用户公开的内容
there are a couple of things you can do:
ReadOnlyCollection
IEnumerable<_type>
the method you use depends on the functionality you need and what you want/need to expose to the user of your class
您拥有的是带有私有访问器的公共财产。这非常有用。它允许实例公开由实例本身控制(设置)的值,例如状态。
例如,采用具有 Count 属性的集合。它有一个公共访问器是没有意义的。一种实现可能是在集合更改时更新属性(内部)(以避免每次都对其进行计数)。
What you have is a public property with a private accessor. It is very useful. It allows an instance to expose a value that is controlled (set) by the instance itself, e.g. a state.
For example, take a collection with a Count property. It makes no sense for it have a public accessor. An implementation could be to update the property (internally) when the collection is changed (to avoid having to count it each time).
执行 setter 方法或将字段包装在另一个类中。这是一个经典的集合set和collection.add问题。
Do a setter method or wrap the field in another class. This is a classic collection set and collection.add problem.