为什么不能在通用IEnumerable对象上调用ToArray? [英] Why can I not call ToArray on a generic IEnumerable object?

查看:131
本文介绍了为什么不能在通用IEnumerable对象上调用ToArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 for ToArray and no extension method ToArray accepting a first argument of type System.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屋!

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