ReSharper的,"返回类型可以是IEnumerable的..."但为什么? [英] Resharper, "Return type can be IEnumerable..." but why?
问题描述
我有像这样的界面......
I have an interface like so...
public interface IAccountRepository : IDisposable {
IQueryable<Account> FindByUserId(int userId); //here, Resharper says "Return type can be IEnumerable<Account>"
}
但ReSharper的是在暗示我将其更改为的IEnumerable< ;帐户> FindByUserId(INT用户id)
代替。
为什么会这样?那岂不是强制加载到内存中整个对象?我认为这将是更好地推迟执行,直到真正需要的对象呢?
Why is that so? Wouldn't it force the entire objects to be loaded into memory? I thought it would be better to defer the execution until the object is really needed?
推荐答案
这只是一个特定的实例一般规则。
This is just a specific instance of a general rule.
如果您正在返回一个类型 SomeClass的
它实现一个接口或从另一个类继承,并在整个解决方案,您只使用返回的对象这是在基类/接口(所以你不要使用 SomeClass的
宣布任何方法),ReSharper的将建议的方法你说,为了基类/接口更换返回的对象类型,使你的代码更通用。
If you are returning a type SomeClass
which implements an interface or inherits from another class, and in your entire solution you only use methods on the returned object which are in the base class/interface (so you don't use any methods declared in SomeClass
), ReSharper will suggest that you replace the type of the returned object with that of the base class/interface in order to make your code more general.
在这种情况下,你只使用方法在的IEnumerable< T>
接口的IQueryable< T>
派生自
In this case, you are only using methods of the IEnumerable<T>
interface that IQueryable<T>
derives from.
还请注意,这仅仅是一个的建议的,而不是一个警告或错误。如果你愿意,你可以放心地忽略这一点。
Note also that this is just a suggestion, not a warning or an error. You can safely ignore this if you want to.
这篇关于ReSharper的,"返回类型可以是IEnumerable的..."但为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!