C# में तत्वों की पहली व्यक्तिगत संख्या प्राप्त करने के लिए Take() विधि का उपयोग करें।
सबसे पहले, एक सूची सेट करें और तत्वों को जोड़ें -
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