Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Item with same key" exception; dictionary with enum keys

Tags:

c#

exception

I've got a Dictionary<TKey,TValue> into which I'm inserting a lot of data. The keys are an enum. I keep getting this exception:

An item with the same key has already been added.

But I don't see any dupes, although I suspect some of my enums have the same value. Would that cause a duplicate key error? I thought an enum was a special type, so it's value wouldn't matter.

The exception isn't very helpful...it doesn't tell me which key is tripping the dupe. I'm using an initializer list.


Edit: Used some regex magic to convert my initializer list into separate .Add calls. Found the key, but I'm still not seeing the dupe...

like image 575
mpen Avatar asked Feb 22 '23 09:02

mpen


1 Answers

If multiple members of your enum has the same numeric representation that would be the cause of exception.

like image 200
Pavel Krymets Avatar answered Mar 06 '23 18:03

Pavel Krymets