如何使用int.TryParse可空INT? [英] How to use int.TryParse with nullable int?

查看:176
本文介绍了如何使用int.TryParse可空INT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的TryParse找到,如果该字符串值是一个整数。如果该值是一个整数,然后跳过foreach循环。这里是我的代码。

I am trying to use TryParse to find if the string value is an integer. If the value is an integer then skip foreach loop. Here is my code.

string strValue = "42 "

 if (int.TryParse(trim(strValue) , intVal)) == false
 {
    break;
 }



INTVAL是一个int类型的变量?(可空INT)。 ?我如何使用的TryParse可空INT

intVal is a variable of type int?(nullable INT). How can I use Tryparse with nullable int?

推荐答案

而无需使用另外一个变量,可惜你不能做到这一点 - 因为类型的退出参数具有参数完全匹配。

You can't do this without using another variable, unfortunately - because the type of out arguments has to match the parameter exactly.

像丹尼尔的代码,但固定在第二个参数方面,修剪,并避免与布尔常量的比较:

Like Daniel's code, but fixed in terms of the second argument, trimming, and avoiding comparisons with Boolean constants:

int tmp;
if (!int.TryParse(strValue.Trim(), out tmp))
{
    break;
}
intVal = tmp;

这篇关于如何使用int.TryParse可空INT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆