有更好看的方法吗? (int 转字符串)

发布于 2024-11-05 01:35:10 字数 747 浏览 0 评论 0原文

有时 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

久随 2024-11-12 01:35:10

().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.

空城缀染半城烟沙 2024-11-12 01:35:10

事实上,我总是使用 Convert.ToString(i + 1),而不是 (i + 1).ToString()。前者是空安全的。

我永远不会使用 value = (0).ToString()

编辑

因为我真的很喜欢这个解决方案,所以我会尽力更好地捍卫它:

优点:

  1. 这是非常常见的习惯用法,并不是特别难读。
  2. 它适用于值类型和引用类型 - 因此它增加了代码凝聚力,因为所有转换看起来完全相同。
  3. 可用于代替大多数类型转换和转换。
  4. 它是空安全的——例如,在处理 DAO 类或 UI 绑定时非常有用。

缺点:

  1. 它比强制转换慢(但不一定比其他类型的转换慢,例如 object.ToString())。
  2. 可能隐藏空引用异常 - 在核心业务类中通常不建议这样做(但同样,很少在此类代码中使用文本到数字的转换)。

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:

  1. It is pretty common idiom, not particularly hard to read.
  2. It works with both value and reference types - so it increases code cohesion, since all conversions look exactly the same.
  3. Can be used in place of most casts and conversions.
  4. It is null-safe - which is very useful when dealing with DAO classes or UI bindings, for instance.

Cons:

  1. It is slower than casts (but not necessarily slower than other kinds of conversions, like object.ToString()).
  2. May hide null reference exceptions - this is usually not recommended in core, business classes (but again, one seldom uses text to numeric conversions in that kind of code).
纸短情长 2024-11-12 01:35:10

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".

爱的故事 2024-11-12 01:35:10

如果您需要将 0 表示为整数,请不要使用字符串,而应使用整数。

至于将计算结果转换为字符串:

var res = string.Format("{0}", i + 1);

您不需要将 () 与文字整数一起使用 - 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:

var res = string.Format("{0}", i + 1);

You don't need to use () with a literal integer - 0.ToString() works.

满天都是小星星 2024-11-12 01:35:10

丑陋是一个多么丑陋的词啊。例如,对我来说,完全主观,但你的写作风格 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.

表情可笑 2024-11-12 01:35:10

我总是使用 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文