Use the Take() method to get the first individual number of elements in C#.
Firstly, set a list and add elements −
List<string> myList = new List<string>();
myList.Add("One");
myList.Add("Two");
myList.Add("Three");
myList.Add("Four");
myList.Add("Five");
myList.Add("Six");Now, use the Take() method to get the elements from the list. Add the number of the elements you want as an argument −
myList.Take(3)
Here is the code −
Example
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);
}
}
}Output
One Two Three