❔ Will I get a performance hit if I use the Enum type as a key value for a Dictionary?

See Code below:
public enum Tee1
{
None
}

public enum Tee2
{
None
}

class Program
{
static Dictionary<Enum, string> h = new Dictionary<Enum, string>();

static void Main(string[] args)
{
h.Add(Tee1.None, "Tee1 string");
h.Add(Tee2.None, "Tee2 string");

System.Console.WriteLine(h[Tee1.None]); // Output : Tee1 string
System.Console.WriteLine(h[Tee2.None]); // Output : Tee2 string
}
}
public enum Tee1
{
None
}

public enum Tee2
{
None
}

class Program
{
static Dictionary<Enum, string> h = new Dictionary<Enum, string>();

static void Main(string[] args)
{
h.Add(Tee1.None, "Tee1 string");
h.Add(Tee2.None, "Tee2 string");

System.Console.WriteLine(h[Tee1.None]); // Output : Tee1 string
System.Console.WriteLine(h[Tee2.None]); // Output : Tee2 string
}
}
Does this also create a hashmap? Or does the compiler do something unexpected here which costs performance?
22 Replies
phaseshift
phaseshift2y ago
What?
the_sea_jay
the_sea_jay2y ago
Since Tee1.None and Tee2.None are both underlying int with the value 0, how does the compiler know the difference between Tee1.None and Tee2.None? I could also do 2 Dictionary with Tee1 as a key and in the other Tee2, my questing was just if I do it the way with Enum type as key , do i Get a performence hit
phaseshift
phaseshift2y ago
Presume it's down to an override of Equals In that case it's the same as any other dictionary Not really following your question. If you have two items with the same hash, they will go in the same bucket But this whole question smacks of premature optimisation
Anton
Anton2y ago
It depends if Enum overrides GetHashCode to just return the integer value. If it does, then yes, they'll end up in the same bucket. You could make a wrapper struct for the key type with that Enum field that does the default behavior of GetHashCode, so (int) _enum xor _enum.GetType().GetHashCode() if you happen to need that. (can't type caret on phone lol)
Aaron
Aaron2y ago
yes, using Enum means that you have to box the values (aka put them on the heap) when it's boxed, it is boxed with the information that the type of the value is Tee1 that is how it knows the difference
Anton
Anton2y ago
in theory they shouldn't box if they are used as constants
Aaron
Aaron2y ago
what
Anton
Anton2y ago
well
Aaron
Aaron2y ago
no (Enum)Tee1.None is a boxing conversion
Anton
Anton2y ago
at least they should reuse the same reference
Aaron
Aaron2y ago
I don't think they do
MODiX
MODiX2y ago
Windows10CE#8553
REPL Result: Success
enum A { None }
object.ReferenceEquals((Enum)A.None, (Enum)A.None)
enum A { None }
object.ReferenceEquals((Enum)A.None, (Enum)A.None)
Result: bool
False
False
Compile: 449.481ms | Execution: 29.987ms | React with ❌ to remove this embed.
Aaron
Aaron2y ago
they do not
Anton
Anton2y ago
hm alright
Aaron
Aaron2y ago
uh, I'm not sure what this is meant to show
wetweezil
wetweezil2y ago
You should be able to click "Expand" otherwise, you will only see the comment at the top
Aaron
Aaron2y ago
I mean yes you can make a dictionary of the types but that doesn't help you when it comes to values
MODiX
MODiX2y ago
Windows10CE#8553
REPL Result: Success
enum E { A, B }
Dictionary<Type, string> dict = new();
dict[E.A.GetType()] = "test";
dict[E.B.GetType()]
enum E { A, B }
Dictionary<Type, string> dict = new();
dict[E.A.GetType()] = "test";
dict[E.B.GetType()]
Result: string
test
test
Compile: 500.192ms | Execution: 34.903ms | React with ❌ to remove this embed.
Aaron
Aaron2y ago
see how both E.A and E.B get the same value because they both have the type E
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts