find index of element c# 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: c# findindex
List<Role> roles = new List<Role>();
roles.Add(new Role() { Name = "Admin", Id = 1 });
roles.Add(new Role() { Name = "User", Id = 2 });
int roleIndex = roles.FindIndex(
role => role.Name == "Admin"
);
string name = "Admin";
int roleIndex = roles.FindIndex(
role => role.Name == "name
);
if (roleIndex > -1)
{
Console.log($"Role {roles[roleIndex].Name} +
was found at index {roleIndex}");
}