<分区>
我正在尝试使用 TryParse 来查找字符串值是否为整数。如果该值为整数,则跳过 foreach 循环。这是我的代码。
string strValue = "42 "
if (int.TryParse(trim(strValue) , intVal)) == false
{
break;
}
intVal 是一个 int?(nullable INT) 类型的变量。如何将 Tryparse 与可为空的 int 一起使用?
标签 c#
<分区>
我正在尝试使用 TryParse 来查找字符串值是否为整数。如果该值为整数,则跳过 foreach 循环。这是我的代码。
string strValue = "42 "
if (int.TryParse(trim(strValue) , intVal)) == false
{
break;
}
intVal 是一个 int?(nullable INT) 类型的变量。如何将 Tryparse 与可为空的 int 一起使用?
最佳答案
这是一个带有 TryParse 的可空 int 选项
public int? TryParseNullable(string val)
{
int outValue;
return int.TryParse(val, out outValue) ? (int?)outValue : null;
}
关于c# - 如何将 int.TryParse 与可为空的 int 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3390750/