A到Z的Enumerable.Range炭名单 [英] A to Z list of char from Enumerable.Range

查看:176
本文介绍了A到Z的Enumerable.Range炭名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打从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.

这篇关于A到Z的Enumerable.Range炭名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆