使用该Take()方法获取C#中的第一个单个元素数。
首先,设置一个列表并添加元素-
List<string> myList = new List<string>();
myList.Add("One");
myList.Add("Two");
myList.Add("Three");
myList.Add("Four");
myList.Add("Five");
myList.Add("Six");现在,使用该Take()方法从列表中获取元素。添加所需元素的数量作为参数-
myList.Take(3)
这是代码-
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<string> myList = new List<string>();
myList.Add("One");
myList.Add("Two");
myList.Add("Three");
myList.Add("Four");
myList.Add("Five");
myList.Add("Six");
//前三个元素
var res = myList.Take(3);
// displaying the前三个元素
foreach (string str in res) {
Console.WriteLine(str);
}
}
}输出结果
One Two Three