有更好看的方法吗? (int 转字符串)
有时 ToString 看起来很丑。
在下面的示例中是否有更聪明的方法来替换 ().ToString() ?
string value;
int i;
...
value = (1 + i).ToString();
其次,你更喜欢什么?
value = "0";
或者
value = (0).ToString();
我知道为了可读性我更喜欢“0”,但另一方面它并不像第二个那样表示值。现在,如果有某种 ToString 运算符...
吹毛求疵,但我现在很无聊。 ;)
附录
这是我编写的代码,导致我提出上面的问题。
List<SelectListItem> result = new List<SelectListItem>(1 + items.Count);
result.Add(new SelectListItem { Text = string.Empty, Value = "0" });
for (int i = 0; i < items.Count; ++i)
{
result.Add(new SelectListItem {
Text = items[i],
Value = (1 + i).ToString()
});
}
Sometimes ToString looks so ugly.
Is there something smarter to replace the ().ToString() with in the following example?
string value;
int i;
...
value = (1 + i).ToString();
Secondly, what do you prefer?
value = "0";
or
value = (0).ToString();
I know I much prefer "0" for readability, but it's on the other hand not representing the value the same way as the second. Now if there was a ToString operator of some kind...
Nitpicking, but I'm bored right now. ;)
Addendum
This is the code I was writing that caused me to ask the question above.
List<SelectListItem> result = new List<SelectListItem>(1 + items.Count);
result.Add(new SelectListItem { Text = string.Empty, Value = "0" });
for (int i = 0; i < items.Count; ++i)
{
result.Add(new SelectListItem {
Text = items[i],
Value = (1 + i).ToString()
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
().ToString()
是完成第一件事的最简洁的方法。任何熟悉表达式工作原理的人在阅读它时都不会有任何困难。执行
(0).ToString()
与执行"a".ToCharArray()
一样愚蠢。如果您有所需值的文字,请使用它。().ToString()
is the most succinct way to do the first thing. Anybody familiar with how expressions work shouldn't have any trouble reading it.And doing
(0).ToString()
is just as silly as doing"a".ToCharArray()
. If you have a literal for the value you want, use it.事实上,我总是使用
Convert.ToString(i + 1)
,而不是(i + 1).ToString()
。前者是空安全的。我永远不会使用
value = (0).ToString()
!编辑
因为我真的很喜欢这个解决方案,所以我会尽力更好地捍卫它:
优点:
缺点:
object.ToString()
)。In fact I always use
Convert.ToString(i + 1)
, instead of the(i + 1).ToString()
. The former is null-safe.And I would never use
value = (0).ToString()
!Edit
Since I'm really fond of that solution, I'll try to defend it better:
Pros:
Cons:
object.ToString()
).ToString 可能对本地化有用 ->
(1000).ToString("N")
。否则,我不明白为什么您要使用(0).ToString()
而不是"0"
。ToString might be usefull for localisation ->
(1000).ToString("N")
. Else, I don't see why you would use(0).ToString()
instead of"0"
.如果您需要将
0
表示为整数,请不要使用字符串,而应使用整数。至于将计算结果转换为字符串:
您不需要将
()
与文字整数一起使用 -0.ToString()
可以工作。If you need to represent a
0
as an integer, don't use a string, use an integer.As for converting the result of a calculation to a string:
You don't need to use
()
with a literal integer -0.ToString()
works.丑陋是一个多么丑陋的词啊。例如,对我来说,完全主观,但你的写作风格
1 + i
完全是错误的。数学家从来不说1+n,他们说n+1。但这只是我的偏好。见仁见智的事情很难从技术上回答。Ugly is such an ugly word. For example, to me, completely subjective, but your style of writing
1 + i
is just plain wrong. Mathematicians never say 1+n, they say n+1. But that's just my preference. Things that are matters of opinion are hard to answer technically.我总是使用
string.Format("{0}", 1 + i)
。我宁愿使用
"0"
,为什么要调用额外的方法?I'm always using
string.Format("{0}", 1 + i)
.I'd rather go with
"0"
, why to call an extra method?