Question by
BonyYousuf · Dec 11, 2016 at 06:29 PM ·
programmingforeachdictionarydevelopmentgarbage-collection
How to traverse a dictionary without foreach that creates boxing?
How to traverse a dictionary without foreach that creates boxing?
Comment
Answer by hexagonius · Dec 11, 2016 at 07:40 PM
I think that kind of question is better placed on stackoverflow
Answer by Flassari · Jan 25, 2017 at 01:58 PM
You have to get the Enumerator yourself, it doesn't create any garbage since the Enumerator is a struct. Foreach boxes it and creates garbage.
// Doing a foreach boxes the enumerator, calling it explicitly doesn't.
var enumerator = myDictionary.GetEnumerator();
while (enumerator.MoveNext())
{
Debug.Log(enumerator.Current.Key);
}
enumerator.Dispose();