我只是想知道如何写一个必须表示为字符串的数字。
例如:
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/