-
What is this line code mean?
allhuman.RemoveAll(s => s == null);
It removes all elements
s
for which the expressions == null
returnstrue
. Or in other words, all elements which fulfill the condition== null
.In those expressions the
s
is like the iterator inforeach(var s in allhuman)
-
can I replace it to the following code?
There is a mistake in your code since you remove elements from the list while iterating it. If you remove one element, next element gets the current index and will be skiped in the next iteration. Example: https://dotnetfiddle.net/CgWDh7
List<object> allhuman = new List<object>{null, null, 1}; for (int i = 0; i < allhuman.Count; i++) { if (allhuman[i] == null) { allhuman.RemoveAt(i); } } allhuman.ForEach(x => Console.WriteLine(x ?? "null"));
=>
null 1
0
solved RemoveAll(s => s == null) What ‘s this line code mean?