int.TryParse = null if not numeric?

前端 未结 8 1461
太阳男子
太阳男子 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:16

    Here's a proper use of Int32.TryParse:

    int? value;
    int dummy;
    if(Int32.TryParse(categoryID, out dummy)) {
        value = dummy;
    }
    else {
        value = null;
    }
    return value;
    

提交回复
热议问题