Get all values of Enum with specific integer value c# -
how values of enum integer value in example
enum colour { red = 1, green = 1, blue = 1, yellow = 2, cyan = 2, purple = 2 } i mean, inputting 1, want output red, green, blue
well, firstly strongly recommend don't this.
having multiple names same value bad idea, imo. however, can reflection:
using system; using system.collections.generic; using system.linq; enum color { red = 1, green = 1, blue = 1, yellow = 2, cyan = 2, purple = 2 } class test { static void main() { foreach (var name in getcolornames(1)) { console.writeline(name); } } static ienumerable<string> getcolornames(int value) { return enum.getnames(typeof(color)) .where(name => (int) enum.parse(typeof(color), name) == value); } } personally have separate values in enum instead, , have lookup<int, color> or that. aside else, confusing have like:
color color = color.blue; ... , see red in debugger or other diagnostics...
Comments
Post a Comment