int.TryPrase
是伟大的,所有的,但只有一个问题......这至少需要两行code使用方法:
INT的intValue;
字符串stringValue的=123;
int.TryParse(stringValue的,出来的intValue);
....
我当然可以这样做:
字符串stringValue的=123;
INT的intValue = Convert.ToInt32(string.IsNullOrWhiteSpace(stringValue的)?0:stringValue的);
上只有一行的code。
我如何可以执行一些魔术得到int.TryParse,使用一个眼线,或者是有但第三种选择在那里?
谢谢!
Bezden回答了这个问题最好的,但在现实中我计划使用Reddogs解决方案。
解决方案 INT的intValue = int.TryParse(stringValue的,出来的intValue)?的intValue:0;
int.TryPrase
is great and all, but there is only one problem...it takes at least two lines of code to use:
int intValue;
string stringValue = "123";
int.TryParse(stringValue, out intValue);
....
Of course I can do something like:
string stringValue = "123";
int intValue = Convert.ToInt32(string.IsNullOrWhiteSpace(stringValue) ? 0 : stringValue);
on just one line of code.
How can I perform some magic to get int.TryParse to use a one liner, or is there yet a third alternative out there?
Thanks!
Bezden answered the question best, but in reality I plan on using Reddogs solution.
解决方案int intValue = int.TryParse(stringValue, out intValue) ? intValue : 0;