int.TryParse = null if not numeric?

前端 未结 8 1492
太阳男子
太阳男子 2021-02-19 10:59

is there some way of return null if it can\'t parse a string to int?

with:

public .... , string? categoryID) 
{
int.TryParse(categoryID, out categoryID);         


        
8条回答
  •  深忆病人
    2021-02-19 11:43

    Simplest and one-liner...

    int N = int.TryParse(somestring, out N) ? N : 0;
    

    It works 'cause it's evaluated left to right. Null not so easy.

提交回复
热议问题