为什么不能在通用IEnumerable对象上调用ToArray? [英] Why can I not call ToArray on a generic IEnumerable object?
问题描述
public static T[] ToArray<T>(IEnumerable<T> e) {
return e.ToArray();
}
我收到以下编译器错误:
I get the following compiler error:
错误1
System.Collections.Generic.IEnumerable< T>
不包含<$$code> ToArray ,没有扩展方法ToArray
接受类型为$$ c $ System.Collections的
第一个参数。可以找到Generic.IEnumerable< T>
(您是否缺少using指令或程序集
引用?
Error 1
System.Collections.Generic.IEnumerable<T>
does not contain a definition forToArray
and no extension methodToArray
accepting a first argument of typeSystem.Collections.Generic.IEnumerable<T>
could be found (are you missing a using directive or an assembly reference?
但是 MSDN参考列出了此方法。这里有什么问题?
But the MSDN reference lists this method. What is wrong here?
推荐答案
假设您使用的是.NET 3.5或更高版本,您需要在代码文件的顶部添加 using
指令:
Assuming you're on .NET 3.5 or later, you need to add the using
directive to the top of your code file:
using System.Linq;
您还需要对<$进行汇编引用c $ c> System.Core (尽管对于Visual Studio项目,默认情况下应该存在)。
You also need to have an assembly reference to System.Core
(although this should be there by default for Visual Studio projects).
这篇关于为什么不能在通用IEnumerable对象上调用ToArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!