ReadOnlyCollection 或 IEnumerable 用于公开成员集合? [英] ReadOnlyCollection or IEnumerable for exposing member collections?

查看:16
本文介绍了ReadOnlyCollection 或 IEnumerable 用于公开成员集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果调用代码仅迭代集合,是否有任何理由将内部集合公开为 ReadOnlyCollection 而不是 IEnumerable?

Is there any reason to expose an internal collection as a ReadOnlyCollection rather than an IEnumerable if the calling code only iterates over the collection?

class Bar
{
    private ICollection<Foo> foos;

    // Which one is to be preferred?
    public IEnumerable<Foo> Foos { ... }
    public ReadOnlyCollection<Foo> Foos { ... }
}


// Calling code:

foreach (var f in bar.Foos)
    DoSomething(f);

在我看来,IEnumerable 是 ReadOnlyCollection 接口的子集,它不允许用户修改集合.因此,如果 IEnumberable 接口就足够了,那么这就是要使用的接口.这是一种正确的推理方式还是我遗漏了什么?

As I see it IEnumerable is a subset of the interface of ReadOnlyCollection and it does not allow the user to modify the collection. So if the IEnumberable interface is enough then that is the one to use. Is that a proper way of reasoning about it or am I missing something?

谢谢/Erik

推荐答案

更现代的解决方案

除非您需要内部集合是可变的,否则您可以使用 System.Collections.Immutable 包,将您的字段类型更改为不可变集合,然后直接公开它 - 当然,假设 Foo 本身是不可变的.

Unless you need the internal collection to be mutable, you could use the System.Collections.Immutable package, change your field type to be an immutable collection, and then expose that directly - assuming Foo itself is immutable, of course.

更新答案以更直接地解决问题

如果调用代码仅迭代集合,是否有任何理由将内部集合公开为 ReadOnlyCollection 而不是 IEnumerable?

Is there any reason to expose an internal collection as a ReadOnlyCollection rather than an IEnumerable if the calling code only iterates over the collection?

这取决于您对调用代码的信任程度.如果您可以完全控制调用此成员的所有内容,并且保证不会使用任何代码:

It depends on how much you trust the calling code. If you're in complete control over everything that will ever call this member and you guarantee that no code will ever use:

ICollection<Foo> evil = (ICollection<Foo>) bar.Foos;
evil.Add(...);

那么可以肯定,直接返回集合是没有坏处的.不过,我通常会尝试比这更偏执一些.

then sure, no harm will be done if you just return the collection directly. I generally try to be a bit more paranoid than that though.

同样,正如你所说:如果你只需要 IEnumerable,那为什么还要把自己绑在更强大的东西上?

Likewise, as you say: if you only need IEnumerable<T>, then why tie yourself to anything stronger?

原答案

如果您使用的是 .NET 3.5,则可以避免复制并且通过使用对 Skip 的简单调用来避免简单的强制转换:

If you're using .NET 3.5, you can avoid making a copy and avoid the simple cast by using a simple call to Skip:

public IEnumerable<Foo> Foos {
    get { return foos.Skip(0); }
}

(有很多其他选项可以简单地包装 - Skip over Select/Where 的好处是没有委托可以为每次迭代毫无意义地执行.)

(There are plenty of other options for wrapping trivially - the nice thing about Skip over Select/Where is that there's no delegate to execute pointlessly for each iteration.)

如果您不使用 .NET 3.5,您可以编写一个非常简单的包装器来做同样的事情:

If you're not using .NET 3.5 you can write a very simple wrapper to do the same thing:

public static IEnumerable<T> Wrapper<T>(IEnumerable<T> source)
{
    foreach (T element in source)
    {
        yield return element;
    }
}

这篇关于ReadOnlyCollection 或 IEnumerable 用于公开成员集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆