int.TryParse syntatic糖 [英] int.TryParse syntatic sugar

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

问题描述

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;

这篇关于int.TryParse syntatic糖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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