String Enums
String Enums behave as regular Enums except they act directly as values. Since they behave exactly the same as Strings, they can be nullable as well.
Goal for String Enums
String enums are useful when you want both an enum type and a string serialization of the enum. Its goal is to model things like: FileOpenMode: "r" | "w" or Encoding: "utf-8" | "utf-16" | "utf-32" | "ascii" | "latin1" | "binary" | "base64" | "hex".
Each value in the Enum is a string literal, but nothing is inferred at runtime.
Inference and Type Checking
From a compiler perspective, if a string literal is being evaluated without a hint, then it will be inferred as a regular string. To infer a String Enum, a hint must be provided, or casting at least.
Casting
Regular casting from StringEnum to String is straightforward, but the reverse is not. To cast a StringEnum to a String, safe casting is recommended:
The safe operator here will automatically check if the value of mode1 is within the bounds of the Mode Enum, and if so, will return the value, otherwise it will return null.
It important to note that the as? operator in this case, will compare against all possible enum values, at run-time thus having an additional run-time cost, so if performance is a concern, it is recommended to implement your own optimized version of the cast.