Tech Off Post

Single Post Permalink

View Thread: Enums are expensive in dictionaries?
  • User profile image
    ktr

    You should use an array. Just make your enum values equal to the corresponding indices.

     

    enum FlyState {
      State1 = 0,
      State2 = 1,
      // ...
    }
    // ...
    var flyStateColors = new Color[Enum.GetValues(typeof(FlyState)).Length];
    // or
    var flyStateColors = new Color[/* hardcoded value */];
    // set a value
    flyStateColors[(int)FlyState.State1] = Color.FromArgb(255, 0, 0);
    // get a value
    var color = flyStateColors[(int)someFlyState];

     

    You could even make extension methods to get/set values of a color array based on FlyState values instead of integer indices.