- Home /
how to get enum value by looking for their hashcode?
for example i have two enums, and each have a value which is the exact same. i noticed that the both values have the same hashcode (for example 29), and now i wanna get a value from the first enum using the hashcode from the same value derived from the second enum. i know that yourenum.getHashCode() can get the hashcode, but how can i look for a value in an enum using this hashcode?
Answer by Glurth · Nov 01, 2017 at 06:20 PM
That sounds crazy! :)
You can use Enum.GetValues(); To get an array that you can foreach though. Then just use GetHashCode() on each to compare them. https://msdn.microsoft.com/en-us/library/system.enum.getvalues(v=vs.110).aspx
here is an untested example of what I mean (you'll probably want to manually cast the returned value, back into the appropriate enum type):
public static object FindHashCodeInEnum(Type enumType,int hashCode)
{
foreach (var value in Enum.GetValues(enumType))
{
if (value.GetHashCode() == hashCode)
return value;
}
return null;
}
yeah, i came up with that, but it seems like i got something wrong there with hashcodes. either way this function works when comparing the value.toString()'s ins$$anonymous$$d of hashcode, so yeah, works :D
Your answer
![](https://koobas.hobune.stream/wayback/20220612142341im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Single enum class 1 Answer