我有一個“玩家”串列,我想知道我的玩家是否是
玩家[0]
或者
球員[1]
或其他。我該怎么做呢?
uj5u.com熱心網友回復:
有一種List<T>.IndexOf
方法可以使用。
uj5u.com熱心網友回復:
linq命令的使用
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; }
}
uj5u.com熱心網友回復:
這個怎么樣:
public int GetIndex(List<PlayerClass> playersList, PlayerClass player)
{
return playersList.FindIndex(a => a == player);
}
如果玩家不在串列中,它應該回傳 -1。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/474357.html