i have a list "players" and i want to find if my player is
players[0]
or
players[1]
or whaterver. How do i do this?
CodePudding user response:
There's a List<T>.IndexOf
method that you can use.
CodePudding user response:
use of linq command
List<Player> players = new List<Player>();
players.Add(new Player() { Id = "1", Name = "angle" });
players.Add(new Player() { Id = "2", Name = "cristin" });
players.Add(new Player() { Id = "2", Name = "robert" });
var finded= players.Where(x => x.Name == "cristin").FirstOrDefault();
public class Player
{
public string Id { get; set; }
public string Name { get; set; }
}
CodePudding user response:
How about this:
public int GetIndex(List<PlayerClass> playersList, PlayerClass player)
{
return playersList.FindIndex(a => a == player);
}
It should return -1 if the player is not in the list.