How to throw an exception when the List <T> .RemoveAll method removes nothing?

I have the following line of code:

races.RemoveAll(br => RaceID == br.raceID);

      

This means removing all races in the race list that match this condition (have the same RaceID) and this line of code works.

However, my question is, how can I throw an exception if NOTHING has been removed?

I want to do:

 if (nothing is removed)
{
throw new Exception ("Nothing was removed from the list, check input");
}

      

+3


source to share


1 answer


RemoveAll

returns a int

value - the number of elements removed by the call.



var elementsRemoved = races.RemoveAll(br => RaceID == br.raceID);
if (elementsRemoved == 0)
{
    throw new Exception ("Nothing was removed from the list, check input");
}

      

+8


source







All Articles