c# find index of item in list code example
Example 1: c# find index element array
int[] arr = { 3, 6, 4, 1, 6, 8 };
Array.IndexOf(arr, 6);
Example 2: c# get index of item in list
Array.IndexOf(arrName, searchingFor)
Example 3: find index of string in a list C#
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List < string > myList = new List < string > () {
"Football",
"Soccer",
"Tennis",
};
int index = myList.FindIndex(a => a.Contains("Tennis"));
Console.WriteLine("List Item Tennis Found at index: " + index);
}
}
Example 4: c# list any retun indec
public class Item {
public int Id { get; set; }
public Item() {}
public Item(int id) { Id = id; }
}
List<Item> idList = { new Item(1), new Item(2), new Item(3) };
Item[] idLArr = [ new Item(1), new Item(2), new Item(3) ];
Item newItem = new Item(1);
int index = idList.FindIndex(item => item.Id == newItem.Id);
int arrIndex = Array.IndexOf(idLArr, newItem.Id);
if (index != -1)
{
}
Example 5: Get Index position of an element in a list in c#
int index = myList.FindIndex(a => a.Prop == oProp);