C# 程序以逆序显示列表中的后三个元素
要显示列表中的后三个元素,请使用 Take() 方法。要对其进行逆序,请使用 Reverse() 方法。
首先,声明一个列表并向其中添加元素 -
List<string> myList = new List<string>();
myList.Add("One");
myList.Add("Two");
myList.Add("Three");
myList.Add("Four");现在,使用 Take() 方法和 Reverse() 以逆序显示列表中的后三个元素 -
myList.Reverse<string>().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");
// first three elements
var res = myList.Reverse<string>().Take(3);
// displaying last three elements
foreach (string str in res) {
Console.WriteLine(str);
}
}
}输出
Four Three Two
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP