Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

Which of the hundreds of alphabets in common use in the world did you have in mind? Which is the rub of course.

The worst problem is that even in languages that adopted the same alphabet, the letter order isn't the same. The Danes put the å first instead of last for example. Crippling properties for an enum, it is impossible to come up with an enum that can represent an alphabet. Unless it is a really obscure one that is used in only one small part of the world or your program doesn't travel very far. Pretty unlikely in the Internet age.

Make sure you don't try, keep it char. And never overlook the need for System.StringComparison

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top