find index in list in c# code example
Example 1: c# get index of item in list
Array.IndexOf(arrName, searchingFor)
Example 2: 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}");
}