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