Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8847883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:17:48+00:00 2026-06-14T12:17:48+00:00

Assume I have some enum. For example enum MyEnum { Item1, Item2, Item3 }

  • 0

Assume I have some enum. For example

enum MyEnum
{ 
    Item1, 
    Item2, 
    Item3
}

I want to “cache” something for each item in the enum. So I do have two options.

Dictionary option:

Dictionary<MyEnum, /*someStructure*/> cache = new Dictionary<MyEnum, /*someStructure*/>>();

or Array option:

/*someStructure*/[] cache = new /*someStructure*/[Enum.GetValues(typeof(MyEnum)).Length]

What are prons and cons of each of these options? In my opinion Dictionary option is more readable and easy to use, but slower than Array option.

But will Dictionary actually be slower? Probably Dictionary is “smart” enough to understand that when enum is used as a key, then just “array” can be used as underling implementation?

So the question is – will “ugly array option” be faster than “straightforward” Dictionary option? Well probably I can just test that… But now when I’ve wrote the question I want to know what others think.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:17:49+00:00Added an answer on June 14, 2026 at 12:17 pm

    Dictionany<TKey, TValue> isn’t “smart” and doens’t optimize for any given key. The underlighing implementation is always the same.

    However, about performance, using enum values as key in dictionary is much slower than you might expect, and is much slower than storing Int32 as the key. The reason for this is because the runtime uses heavy reflection to get the hash code of an enum when calling GetHashCode(). This if actually found quite bizar.

    But all of this doesn’t matter when the most readable approach (using the enum as key in a dictionary) is fast enough. And nobody here can answer that question for you. You will have to measure this. Don’t do premature optimization and go with the most readable/maintainable pease of code until proven that the solution isn’t fast enough for in your situation (which it will probably be).

    However, instead of switching to an array, try switching to a dictionary with a Int32 key:

    var dictionary = new Dictionary<int,  /*someStructure*/>();
    
    dictionary[(int)MyEnum.Item1] = /*new someStructure()*/;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have an enumerable object enum and now I want to get the
Assume I have some enum like the following: enum Towns { Rome, Napoli, Modena
Just assume I have some class Foo, that has two dependencies: an ISerializer<T> and
let's assume I have some class A and derived from it B: I want
Assume I have some simple class class TestClass: def doSomething(self): print 'Did something' I
Assume I have some utility that can take a number of options, each followed
Let's assume I have some MyObject obj object and want to access a property
I have some question about linux-kernel, let's assume that I have two threads in
Assume I have some program that pulls airline data. Each airline data uses a
Let's assume I have some method that executes CLI application. For example: public string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.