C# 程序获取列表中的前三个元素
使用 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");
// first three elements
var res = myList.Take(3);
// displaying the first three elements
foreach (string str in res) {
Console.WriteLine(str);
}
}
}输出
One Two Three
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP