Another thing you may want to do is ensure the strings don’t contain any invalid characters or keywords that would break in C#. So you can combine some of the other answers here with something like this:
private static HashSet<string> csharpKeywords = new HashSet<string> {
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked",
"class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else",
"enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for",
"foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock",
"long", "namespace", "new", "null", "object", "operator", "out", "override", "params",
"private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed",
"short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw",
"true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using",
"virtual", "void", "volatile", "while"
};
// given a string, attempts to make the string compatible with an enum
static string MakeStringEnumCompatible(string text)
{
if (text.Length <= 0) { return "INVALID_ENUM_NAME"; }
string ret = "";
// first char must be a letter or an underscore
if (char.IsLetter(text[0]) || (text[0] == '_')) { ret += text[0]; }
// strip out anything that's not a digit or underscore
for (int i = 1; i < text.Length; ++i) {
if (char.IsLetterOrDigit(text<em>) || (text <em>== '_')) { ret += text*; }*</em></em>
* }*
* if (ret.Length <= 0) { return “INVALID_ENUM_NAME”; }
_ // all the keywords are lowercase, so if we just change the first letter to uppercase,
// then there will be no conflict*
* if (csharpKeywords.Contains(ret)) { ret = char.ToUpper(ret[0]) + ret.Substring(1); }*_
* return ret;*
* }*