0

我有以下代码:

    [TestMethod]
    public void A_Player_Can_Be_Deleted_From_The_List()
    {                     
        Player player = playerList.Find(ByName("Davy",whatGoesHere?);
        playerList.Remove(player);

        playerList.Count.Should().Be(2);
    }

Func 'ByName' 定义为:

Func<string, Player, bool> ByName = (name, player) => player.Name == name;

我不知道如何传递第二个(玩家)参数。是否可以?

4

2 回答 2

1

player提供给您传递给的谓词,List.Find<T>因此您可以将其传递给ByName

Player player = playerList.Find(p => ByName("Davy", p));
playerList.Remove(player);

而不是使用FindandRemove你可以使用List<T>.RemoveAllie

playerList.RemoveAll(p => ByName("Davy", p));
于 2012-09-22T15:45:10.367 回答
1

如果要ByName在 Method 中使用委托,则Find必须为列表中的每个元素执行它。例如:

string player = playerList.Find((playerElement) => ByName("Davy", playerElement));
于 2012-09-22T15:47:35.440 回答