拥有一个带有私有访问器的公共字段有意义吗?

发布于 2025-01-03 22:57:52 字数 641 浏览 3 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

帅的被狗咬 2025-01-10 22:57:52

您可以将列表包装在 ReadOnlyCollection中并公开:

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; }
}

在内部,您可以访问 dipendenti 并可以添加/删除项目。外部实体只能访问 ReadOnlyCollection对象。包装列表,因此他们只能读取,但不能添加/删除项目。

You can wrap the list in a ReadOnlyCollection<T> and expose that:

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. External entities have access only to the ReadOnlyCollection<T> that wraps the list, so they can only read, but not add/remove items.

很酷又爱笑 2025-01-10 22:57:52

我同意 dtb 的观点,即 ReadOnlyCollections 是可行的方法。但是,您可以从属性 getter(使用 AsReadOnly)返回它并删除该方法。

    private List<Dipendente> dipendenti = new List<Dipendente>();

    public ReadOnlyCollection<Dipendente> ReadOnlyDipendenti
    {
        get
        {
            return dipendenti.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.

    private List<Dipendente> dipendenti = new List<Dipendente>();

    public ReadOnlyCollection<Dipendente> ReadOnlyDipendenti
    {
        get
        {
            return dipendenti.AsReadOnly(); 
        }
    }
坚持沉默 2025-01-10 22:57:52

您可以执行以下几项操作:

  • 您使用ReadOnlyCollection
  • 您可以返回 IEnumerable<_type>
  • 您可以将列表包装在另一个类中
  • 您可以推出自己的集合类,实现适当的接口

    您使用的方法取决于您需要的功能以及您想要/需要向类的用户公开的内容

  • there are a couple of things you can do:

  • you use ReadOnlyCollection
  • you can return an IEnumerable<_type>
  • you can wrap the list in another class
  • you can roll your own collection class, implementing the appropriate interface

    the method you use depends on the functionality you need and what you want/need to expose to the user of your class

  • 沉鱼一梦 2025-01-10 22:57:52

    您拥有的是带有私有访问器的公共财产。这非常有用。它允许实例公开由实例本身控制(设置)的值,例如状态。

    例如,采用具有 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).

    万劫不复 2025-01-10 22:57:52

    执行 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.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文