C#Take operator

use*_*522 1 .net c# linq

鉴于以下声明

int[] numbers = { 0, 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)

我注意到.NET 4.6.1上的VS 2015允许以下内容采用前三个数字

IEnumerable<int> firstThree = System.Linq.Enumerable.Take(numbers, 3);
Run Code Online (Sandbox Code Playgroud)

但是,它不允许以下内容

IEnumerable<int> firstThree = numbers.Take(3);
Run Code Online (Sandbox Code Playgroud)

我是C#的新手,无法理解为什么调用没有解析为扩展方法.

Ami*_*ich 5

Take是一种可以在命名空间 下找到的扩展方法System.Linq.

您只需要在文件中包含该命名空间:

using System.Linq;
Run Code Online (Sandbox Code Playgroud)

然后你就可以使用扩展方法了.