枚举字母的最快方法 [英] Quickest way to enumerate the alphabet
本文介绍了枚举字母的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想像这样遍历字母:
foreach(char c in alphabet)
{
//do something with letter
}
最好是一个字符数组做到这一点的方法? (感觉很hacky)
Is an array of chars the best way to do this? (feels hacky)
编辑:度量标准是最少键入以实现,同时仍保持可读性和健壮性
The metric is "least typing to implement whilst still being readable and robust"
推荐答案
(假设ASCII等)
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
}
这篇关于枚举字母的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文