एक ही पंक्ति में किसी आइटम का इंडेक्स प्राप्त करने के लिए, FindIndex() और Contains() विधि का उपयोग करें।
int index = myList.FindIndex(a => a.Contains("Tennis"));
उपरोक्त, हमने FindIndex() का उपयोग करके किसी तत्व का इंडेक्स प्राप्त किया, जिसमें उस विशिष्ट तत्व के लिए Contains विधि द्वारा सहायता की गई है।
यहां पूरा कोड है:
उदाहरण
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List myList = new List() {
"Football",
"Soccer",
"Tennis",
};
// इंडेक्स ढूंढना
int index = myList.FindIndex(a => a.Contains("Tennis"));
// इंडेक्स प्रदर्शित करना
Console.WriteLine("List Item Tennis Found at index: " + index);
}
}
आउटपुट
List Item Tennis Found at index: 2