Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"for loop" for ValueCollection

Tags:

c#

Is there a way to iterate ValueCollection using straight for loop? (not foreach)

e.g.

Dictionary<string, List<long>>.ValueCollection somevalues = somecollection.Value.Values;

for(int i = 0; i< somevalues.Count; i++)
{
 //now what?
}
like image 344
ra170 Avatar asked Feb 12 '26 16:02

ra170


1 Answers

No - there's no indexer or anything similar for ValueCollection. I suppose you could use:

for (int i = 0; i < someValues.Count; i++)
{
    var item = someValues.ElementAt(i);
    ...
}

but that would perform like a dog :) (Just iteration would be O(n^2), as it would be like Schlemiel the Painter.)

Why do you want to do this? If you want items with elements, just keep a counter or use the overload of Select that passes in the index as well as the item.

like image 172
Jon Skeet Avatar answered Feb 15 '26 04:02

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!