最佳类型设置为返回类型为返回一个集合的方法? [英] Best Type to set as return type for methods that return a collection?

查看:189
本文介绍了最佳类型设置为返回类型为返回一个集合的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是最好的类型给我们返回的集合?

Which is the best type to us for returning collections?

我应该使用的IList< T> 的IEnumerable< T> 的IQueryable< T> ,别的东西吗?这是最好的和为什么

Should I use IList<T>, IEnumerable<T>, IQueryable<T>, something else? Which is best and why?

我试图来决定,我应该使用通常,无论是在界面和我正在写了几类的实现。

I'm trying to decide which I should use typically, both in the interface and the implementation of a few classes that I'm writing.

修改我,我正在使用LINQ to SQL来了一个WCF服务返回的数据钉下来了一点。这感觉就像,可能使在最佳类型的改变使用?

edit Let me nail this down a little further, I am using LINQ to SQL to return data over a WCF Service. It feels like that may make a change in the best type to use?

推荐答案

我默认为IEnumerable的。我拍摄的最小接口暴露。无论的IList&LT; T&GT; 的IQueryable&LT; T&GT; 实施的IEnumerable&LT; T&GT; 。所以,除非你有方法等具体要求,我会去的极简主义,用最少的派生类型。如果你有你的电话code其他要求,如索引查找的性能,或获取集合中的项目数,那么你可能想选择另一种类型,如的ICollection&LT; T&GT;

I default to IEnumerable. I'm shooting for the minimal interface to expose. Both IList<T> and IQueryable<T> implement IEnumerable<T>. So unless you have other specific requirements for the methods I'd go for minimalism and use the least derived type. If you have other requirements in your calling code, such as performance of indexed lookups or getting the number of items in the collection then you might want to choose another type such as ICollection<T>.

这篇关于最佳类型设置为返回类型为返回一个集合的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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