Pradeep Singh Pradeep Singh - 29 days ago 9
C# Question

list.removeall argument null exception not thrown

I have list of type struct in which i temporarily add data and delete them when data is being written either to database or to a file, I am using

list.RemoveAll
method to remove item from the list.

The
list.RemoveAll
should throw
ArgumentNullException
if I call it on a list whose count is zero but it is not throwing any exception even if the count is zero.

_dataNotWrittenToDb.RemoveAll(item => item.ScopeId == _a2Data.ScopeId);
_dataWrittenToDB.RemoveAll(item => item.ScopeId == _a2Data.ScopeId);

Answer

From MSDN

Exceptions

Exception   Condition
ArgumentNullException   

match is null.

RemoveAll throw ArgumentNullException only when passed parameter is null, in your case it is not null

RemoveAll's job is to remove all elements that match to given condition, in your case no matching elements, no removes and no reason to return exception