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"); // पहले तीन तत्व var res = myList.Take(3); // पहले तीन तत्व प्रदर्शित करना foreach (string str in res) { Console.WriteLine(str); } } }आउटपुट
One Two Three