c# - Custom names on enum elements -


suppose there's enum so.

[flags] public enum crazies {   1 = 2,   2 = 3,   3 = 4 } 

now, when force different values stringified, text in beautiful tongue, differ considerably usual meaning of numerics. suppose users prefer more conventional definition of natural numbers , wish see text "two", although variable value one (nb it's crazies.one).

what'd simplest way map stringification of predefined names of elements? right i'll go following don't it.

private static string decrazyfycrazies(crazies wrong) {   switch(wrong)   {     case crazies.one: return "two";     case crazies.two: return "three";   }   return "four"; } 

if need int cast int

return (int)crazies.one; 

for more custom string display can use display name

i.e

public enum crazies {   [display(name="custom name one")]   1 = 2,   [display(name="custom name two")]   2 = 3      } 

see more power enum


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -