我有以下代码行:
races.RemoveAll(br => RaceID == br.raceID);
这是为了删除Race List中符合此条件的所有种族(具有相同的RaceID),并且这行代码有效。
但是,我的问题是,如果删除NOTHING,如何抛出异常?
我想这样做:
if (nothing is removed)
{
throw new Exception ("Nothing was removed from the list, check input");
}
答案 0 :(得分:8)
RemoveAll
返回int
值 - 调用删除的元素数。
var elementsRemoved = races.RemoveAll(br => RaceID == br.raceID);
if (elementsRemoved == 0)
{
throw new Exception ("Nothing was removed from the list, check input");
}