gpt4 book ai didi

c# - 字符串和数字条件的最佳实践

转载 作者:太空狗 更新时间:2023-10-29 22:20:57 27 4
gpt4 key购买 nike

我只是想知道如何写一个必须表示为字符串的数字。

例如:

if (SelectedItem.Value == 0.ToString()) ...

if (SelectedItem.Value == "0") ...

public const string ZeroNumber = "0";
if (SelectedItem.Value == _zeroNumber) ...

if (Int.Parse(SelectedItem.Value) == 0)

最佳答案

对于单个测试,我个人会选择

if (SelectedItem.Value == "0")

它没有大惊小怪,没有仪式感 - 它准确地说明了您想要做的事情。

另一方面,如果我有一个应该是数字的值,然后我会根据该数字使用react,我会使用:

int value;
// Possibly use the invariant culture here; it depends on the situation
if (!int.TryParse(SelectedItem.Value, out value))
{
// Throw exception or whatever
}
// Now do everything with the number directly instead of the string

关于c# - 字符串和数字条件的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2235603/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com