C# Linq First() 方法
csharpprogrammingserver side programming
使用 First() 方法从数组中获取第一个元素。
首先,设置一个数组。
int[] arr = {20, 40, 60, 80 , 100};
现在,使用 Queryable First() 方法返回第一个元素。
arr.AsQueryable().First();
以下是整个示例。
示例
using System; using System.Linq; using System.Collections.Generic; class Program { static void Main() { int[] arr = {20, 40, 60, 80 , 100}; // 获取第一个元素 int res = arr.AsQueryable().First(); Console.WriteLine(res); } }
输出
20