开发者

How do I call TryParse from within a Predicate

开发者 https://www.devze.com 2022-12-21 11:04 出处:网络
How do I use the TryParse method within a Predicate?TryParse requires an out parameter.In the example below, I would like to call TryParse to determine if x can be c开发者_运维百科onverted to an integ

How do I use the TryParse method within a Predicate? TryParse requires an out parameter. In the example below, I would like to call TryParse to determine if x can be c开发者_运维百科onverted to an integer. I really don't care for the out parameter--I just want to get this to compile.

        string[] nums = num.Split('.');
        PexAssume.TrueForAll(nums, x => int.TryParse(x, out (int)0));


string[] nums = num.Split('.');
PexAssume.TrueForAll(nums, x => { int result; return int.TryParse(x, out result); });

The "expression" part of a lambda can be wrapped in braces, allowing a full function body with multiple statements. So long as the result of that body is the same as the return value of the implied function you are implementing, you can do whatever you need to do between those braces.


If you don't care about the output, you can do it like this:

string[] nums = num.Split('.');
int unused;
PexAssume.TrueForAll(nums, x => int.TryParse(x, out unused)); 
0

精彩评论

暂无评论...
验证码 换一张
取 消