[英]Does having a public field with private accessors make sense?
I have a class called GestorePersonale
which holds a list of instances of another class: 我有一个名为
GestorePersonale
的类,它包含另一个类的实例列表:
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.
List<Dipendente>
本身上执行任何其他类型的操作。
I considered converting the first code snippet to 我考虑过将第一个代码段转换为
\nprivate List dipendenti;私人清单;
but I could find a few downsides to that: 但我可以找到一些缺点:
What would be the best way to solve this situation? 什么是解决这种情况的最佳方法?
You can wrap the list in a ReadOnlyCollection<T> and expose that: 您可以将列表包装在ReadOnlyCollection <T>中并公开:
private List<Dipendente> dipendenti;
private ReadOnlyCollection<Dipendente> readOnlyDipendenti;
public GestorePersonale()
{
dipendenti = new List<Dipendente>();
readOnlyDipendenti = new ReadOnlyCollection<Dipendente>(dipendenti);
}
public ReadOnlyCollection<Dipendente> Dipendenti
{
get { return readOnlyDipendenti; }
}
Internally, you have access to dipendenti
and can add/remove items. 在内部,您可以访问
dipendenti
并可以添加/删除项目。 External entities have access only to the ReadOnlyCollection<T> that wraps the list, so they can only read, but not add/remove items. 外部实体只能访问包装列表的ReadOnlyCollection <T>,因此它们只能读取,但不能添加/删除项目。
I would agree with dtb that ReadOnlyCollections is the way to go. 我同意dtb,ReadOnlyCollections是要走的路。 However, you can return it from the property getter (using AsReadOnly) and drop the method.
但是,您可以从属性getter(使用AsReadOnly)返回它并删除该方法。
private List<Dipendente> dipendenti = new List<Dipendente>();
public ReadOnlyCollection<Dipendente> ReadOnlyDipendenti
{
get
{
return dipendenti.AsReadOnly();
}
}
there are a couple of things you can do: 你可以做几件事:
ReadOnlyCollection
ReadOnlyCollection
IEnumerable<_type>
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 您使用的方法取决于您需要的功能以及您希望/需要向您的类用户公开的内容
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, eg a state.
它允许实例公开由实例本身控制(设置)的值,例如状态。
For example, take a collection with a Count property. 例如,使用Count属性获取集合。 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).
实现可以是在更改集合时更新属性(内部)(以避免每次都计数)。
Do a setter method or wrap the field in another class. 执行setter方法或将字段包装在另一个类中。 This is a classic collection set and collection.add problem.
这是一个经典的集合集和collection.add问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.