Search code examples
c#.netlinqenumsbase-class-library

Is there an enum type for letters in .NET framework?


I need enum type that defines letters for using in a project. Is there such an enum type in .NET framework ?


Solution

  • Given that there's no enum available, a quick way to generate an enumerable (IEnumerable of Char) that contains all the letters of a character set is...

     var letters = Enumerable.Range(0, 255).Select(Convert.ToChar).Where(Char.IsLetter);
    

    You can 'or' the method group with IsDigit or IsPunctuation to include additional letters.

    Note: there are limitations and assumptions to this approach. Check these...

    The Unicode characters are divided into categories. A character's category is one of its properties. For example, a character might be an uppercase letter, a lowercase letter, a decimal digit number, a letter number, a connector punctuation, a math symbol, or a currency symbol. The UnicodeCategory class returns the category of a Unicode character. For more information on Unicode characters, see the Unicode Standard.

    Source: How to determine if a CultureInfo instance supports Latin characters

    The .NET Framework uses the Char structure to represent a Unicode character. The Unicode Standard identifies each Unicode character with a unique 21-bit scalar number called a code point, and defines the UTF-16 encoding form that specifies how a code point is encoded into a sequence of one or more 16-bit values. Each 16-bit value ranges from hexadecimal 0x0000 through 0xFFFF and is stored in a Char structure. The value of a Char object is its 16-bit numeric (ordinal) value.

    Source: http://msdn.microsoft.com/en-us/library/System.Char(v=vs.110).aspx