fork download
  1. using System;
  2.  
  3. [Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };
  4.  
  5. public class Example
  6. {
  7. public static void Main()
  8. {
  9. string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
  10. foreach (string colorString in colorStrings)
  11. {
  12. try {
  13. Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString);
  14. if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))
  15. Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
  16. else
  17. Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
  18. }
  19. catch (ArgumentException) {
  20. Console.WriteLine("'{0}' is not a member of the Colors enumeration.", colorString);
  21. }
  22. }
  23. }
  24. }
Success #stdin #stdout 0.02s 38120KB
stdin
Standard input is empty
stdout
Converted '0' to None.
Converted '2' to Green.
8 is not an underlying value of the Colors enumeration.
'blue' is not a member of the Colors enumeration.
Converted 'Blue' to Blue.
'Yellow' is not a member of the Colors enumeration.
Converted 'Red, Green' to Red, Green.