I have a list: List<MyClass> MyList = new List<MyClass>();.
MyClass has methods SetBool and IsTrue. I have to set every object of this list to false (obj.SetBool(false)).
There are 2 possible ways:
First:
foreach (MyClass obj in MyList)
{
obj.SetBool(false)
}
Second:
List<MyClass> MyList2 = MyList.Where(c => c.IsTrue()).ToList();
foreach (MyClass obj in MyList2)
{
obj.SetBool(false)
}
If I use first one it may be slow because it changes every element. The second way can be slow too, because it has to find objects first.
So my question is:
Which one will be faster (I may have very large number of elements in list) and why?
Fastest would be the combination:
But the difference with your first version will only be meaningful when
SetBool()does some extensive validations or computation, and is much more expensive thanIstrue().