C# List IList 或 IEnumerable 作为参数

C# List IList or IEnumerable as argument

如果我想将 List 作为方法的参数,Resharper 会提示使用 IList 接口可能是更好的主意。如果我随后将 List 替换为 IList,Resharper 会建议使用 IEnumerable。在 Java 中,我还将 List 接口而不是 ArrayList 作为参数。但是使用 iEnumerable 作为参数真的是好看的代码吗?如果不需要 IList 或 List 的功能,那么使用 IEnumerable 是一种好习惯吗?

这是 the article 让我改变了我的想法并让我开始遵循

的原则

take the most generic type, return the most specific type

所以回答你的问题

Is this good practice to use IEnumerable if no functionality of IList or List are required?

是的,是

注意:不要过度!我见过接受 IEnumerable 的代码,如果参数不是 List,则抛出异常。这是愚蠢的。我上面引用的这句话的意思是"don't ask for more specificity than you need"