返回IOrderedEnumerable< T& gt;的方法是否有利?而不是IEnumerable< T& gt ;? [英] Can it be advantageous for a method to return IOrderedEnumerable<T> instead of IEnumerable<T>?

查看:143
本文介绍了返回IOrderedEnumerable< T& gt;的方法是否有利?而不是IEnumerable< T& gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

返回IOrderedEnumerable而不是IEnumerable的方法是否有好处?

Can it be advantageous for a method to return IOrderedEnumerable instead of IEnumerable?

推荐答案

仅当您希望人们每次都订购该枚举并且很难弄清楚如何执行此操作时,或者您可以提供一个实现该功能的集合时,界面,可以有效地对其内容进行排序,并与可以识别您的收藏的扩展方法配对.

Only if you expect people to order that enumerable every time and would find it hard to figure out how to do this OR if you can provide a collection that implements that interface that can efficiently order its contents and is paired with an extension method that is aware of your collection.

最佳选择是返回特定的集合类型(有关详细信息,请参阅Richter).如果有一个简单的可枚举的对象,则100个对象中有99个可以使用标准的linq扩展方法对其进行排序.

Best option is to return a specific collection type (see Richter for details on that). 99 times out of 100 whoever gets even a simple enumerable can use the standard linq extension methods to order it if they want.

这篇关于返回IOrderedEnumerable< T& gt;的方法是否有利?而不是IEnumerable< T& gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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