/// <summary> /// Determines whether a particular string is a valid C# identifier. Legal /// identifiers must start with a letter, and all the characters must be /// letters or numbers. Underscore is considered a letter. /// </summary> private static bool IsLegalIdentifier ( string identifier ) { // Must be non-empty. if (identifier.Length == 0) { return(false); } // First character must be a letter. // From 2.4.2 of the C# Language Specification // identifier-start-letter-character: if ( !TokenChar.IsLetter(identifier[0]) && (identifier[0] != '_') ) { return(false); } // All the other characters must be letters or numbers. // From 2.4.2 of the C# Language Specification // identifier-part-letter-character: for (int i = 1; i < identifier.Length; i++) { char currentChar = identifier[i]; if ( !TokenChar.IsLetter(currentChar) && !TokenChar.IsDecimalDigit(currentChar) && !TokenChar.IsConnecting(currentChar) && !TokenChar.IsCombining(currentChar) && !TokenChar.IsFormatting(currentChar) ) { return(false); } } return(true); }
private static bool IsLegalIdentifier(string identifier) { if (identifier.Length == 0) { return(false); } if (!TokenChar.IsLetter(identifier[0]) && (identifier[0] != '_')) { return(false); } for (int i = 1; i < identifier.Length; i++) { char c = identifier[i]; if (((!TokenChar.IsLetter(c) && !TokenChar.IsDecimalDigit(c)) && (!TokenChar.IsConnecting(c) && !TokenChar.IsCombining(c))) && !TokenChar.IsFormatting(c)) { return(false); } } return(true); }