I have a Dictionary<String, Object>
with some data in it.
I want to loop through each element, I know I can use foreach
for that.
But I little bit concerned about the performance and the memory usage for it.
But here's the thing, we know that for (;;)
is faster and use less RAM ( if I remember correctly ) than foreach ()
since it doesn't create new instance for each element.
But we can't use for (;;)
directly to iterate through it, so we need LINQ's ElementAt()
or something similar for it.
So which is more efficient, is using foreach ()
more efficient
private Dictionary <String, Object> items;
foreach (var entry in items)
{
// do something with entry
}
or using for (;;)
combined with LINQ is more efficient
private Dictionary <String, Object> items;
for (var c = 0; c < items.Count; c++)
{
// do something with items.ElementAt(c)
}
or for (;;)
combined with LINQ is the same as foreach ()
because items.ElementAt()
may also create instance ?
EDIT : If there's discussion discussing about it and maybe has the answer, feel free to mark this as duplicate, but let me know the reference.