IEnumerable as return type

前端 未结 10 2297
轮回少年
轮回少年 2020-12-01 10:41

Is there a problem with using IEnumerable as a return type? FxCop complains about returning List (it advises returning Coll

10条回答
  •  一个人的身影
    2020-12-01 11:12

    No, IEnumerable is a good thing to return here, since all you are promising is "a sequence of (typed) values". Ideal for LINQ etc, and perfectly usable.

    The caller can easily put this data into a list (or whatever) - especially with LINQ (ToList, ToArray, etc).

    This approach allows you to lazily spool back values, rather than having to buffer all the data. Definitely a goodie. I wrote-up another useful IEnumerable trick the other day, too.

提交回复
热议问题