[Solved] Optimizing foreach loop that compares multiple things [duplicate]


If you have a IEnumberable of KeyValuePairs then you could do something like this:

public bool AreAllSame(IEnumberable<KeyValuePair<Int64, MyObject>> list)
{
  return list.Select(kv => kv.Value).Distinct().Count == 1;
}

Not sure whether it’s really optimized but, it’s shorter! :-]

Of course, whatever you’re comparing will need to be comparable.

2

solved Optimizing foreach loop that compares multiple things [duplicate]