Question by
jyzgo · Aug 27, 2015 at 03:41 AM ·
foreachcollections
If foreach loop isn't recommended,which container should I use to store unsorted data?
Hi guys, I need iterate a collection in Update(),I don't care about the order,so I used HashSet to store them and use foreach loop to iterate them ,but I found that foreach has some GC problem ,so I wonder which collection is best practice in this case ?
void Update()
{
foreach (var ele in _curHashSet)
{
//do sth.....
}
}
and the reason I use HashSet is this collection will be Add and Remove frequently~
Comment
Answer by cjdev · Aug 27, 2015 at 07:09 AM
According to this source HashSets don't allocate in foreach loops so you shouldn't have a problem with GC. Looping through a large collection in an Update loop might be problem enough though...
Thank u ! I just find the answer, thanks anyway : )
http://answers.unity3d.com/questions/697498/any-way-to-use-foreachfor-with-dictionary-without.html