I want to iterate over the alphabet like so:
foreach(char c in alphabet) { //do something with letter }
Is an array of chars the best way to do this? (feels hacky)
Edit: The metric is "least typing to implement whilst still being readable and robust"
Total number of letters in the alphabet Until 1835, the English Alphabet consisted of 27 letters: right after "Z" the 27th letter of the alphabet was ampersand (&). The English Alphabet (or Modern English Alphabet) today consists of 26 letters: 23 from Old English and 3 added later.
There are 26 letters in the English alphabet which range from 'a' to 'z' (with b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, and y in between).
The easiest way to load a list of all the letters of the alphabet is to use the string. ascii_letters , string. ascii_lowercase , and string. ascii_uppercase instances.
While the numbers were lost over time, the letters and their order remain. Psychology Today posts that it had something to do with how easy it is to memorize with its bouncy cadence. Another theory is that the letters were put in order as part of a memory tool so people could memorize all 26 of them.
(Assumes ASCII, etc)
for (char c = 'A'; c <= 'Z'; c++) { //do something with letter }
Alternatively, you could split it out to a provider and use an iterator (if you're planning on supporting internationalisation):
public class EnglishAlphabetProvider : IAlphabetProvider { public IEnumerable<char> GetAlphabet() { for (char c = 'A'; c <= 'Z'; c++) { yield return c; } } } IAlphabetProvider provider = new EnglishAlphabetProvider(); foreach (char c in provider.GetAlphabet()) { //do something with letter }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With