목록에서 마지막 세 요소를 표시하려면 Take() 메서드를 사용합니다. 반대로 하려면 Reverse() 메서드를 사용하세요.
먼저 목록을 선언하고 요소를 추가하십시오 -
List<string> myList = new List<string>();
myList.Add("One");
myList.Add("Two");
myList.Add("Three");
myList.Add("Four"); 이제 Reverse()와 함께 Take() 메서드를 사용하여 목록의 마지막 세 요소를 역순으로 표시합니다. -
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