我只是想知道如何编写一个必须表示为字符串的数字.
例如:
if (SelectedItem.Value == 0.ToString()) ...
Run Code Online (Sandbox Code Playgroud)
要么
if (SelectedItem.Value == "0") ...
Run Code Online (Sandbox Code Playgroud)
要么
public const string ZeroNumber = "0";
if (SelectedItem.Value == _zeroNumber) ...
Run Code Online (Sandbox Code Playgroud)
要么
if (Int.Parse(SelectedItem.Value) == 0)
Run Code Online (Sandbox Code Playgroud)
对于单个测试,我个人会去
if (SelectedItem.Value == "0")
Run Code Online (Sandbox Code Playgroud)
它没有大惊小怪,也没有仪式 - 它说的正是你想要做的.
另一方面,如果我有一个应该是数字的值,然后我将根据该数字作出反应,我会使用:
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
Run Code Online (Sandbox Code Playgroud)