A到Z的Enumerable.Range炭名单名单、Enumerable、Range

2023-09-03 00:34:53 作者:短歌

我想打从Enumerable.Range列表。这是code正确吗?

I want to make a list from Enumerable.Range. Is this code correct?

SurnameStartLetterList = new List<char>();
Enumerable.Range(65, 26).ToList().ForEach(character => SurnameStartLetterList.Add((char)character));

还是有更好的方法,使这种类型的列表?

Or is there a better way to make this type of list?

在此先感谢:)

推荐答案

嗯,字符串的IEnumerable&LT;焦炭&GT; ,所以这也将工作:

Well, string is IEnumerable<char>, so this would also work:

"ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToList()

您必须权衡的利弊在此。

You have to weigh the pros and cons on this.

优点:

更易于阅读以上code比你的循环(主观的,这是我的意见) 在较短的code(但可能没有足够的占多)

缺点:

难读,如果你不知道什么 .ToList()将做一个字符串

可以引入错误,例如,你会很容易在这里发现的错误: Harder to read if you don't know what .ToList() will do with a string

Can introduce bugs, for instance, would you easily spot the mistake here:

"ABCDEFGHIJKLMN0PQRSTUVWXYZ".ToList()

通过轻松我的意思是,你会发现的错误,因为你只是读过去的code,如果你不知道有一个问题在这里和去打猎吧。

By easily I mean that you would spot the mistake as you're just reading past the code, not if you knew there was a problem here and went hunting for it.