Dictionaries and Enum Keys
Introduction
Enums are a very common type of object used in dictionaries. Unfortunately there is a hidden behavior in dictionaries that use enums as their keys which can cause performance problems - usually resulting from memory allocation.
Our example
Consider the following simple example:
While that may seem like code that shouldn't case problems, unfortunately under the hood .NET casts the enum to an object to perform the equality check. This cast takes far more time than an integer comparison, and it also creates garbage, which can be fatal on the Xbox 360. If you'd like to read more, check this blog post.
How to fix it
The easiest way to fix this problem is to simply use ints and cast your enums as follows:
This will completely eliminate any allocation that will occur in your ContainsKey call.
Last updated