how to know how many indexes there are in a list c3 code example
Example 1: get both item and index in c#
public static IEnumerable<(T item, int index)> WithIndex<T>(this IEnumerable<T> source)
{
return source.Select((item, index) => (item, index));
}
foreach (var (item, index) in collection.WithIndex())
{
DoSomething(item, index);
}
Example 2: 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)
{
}