int.TryParse = null if not numeric?

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

    How about this?

    public int? ParseToNull(string categoryId)
    {
        int id;
        return int.TryParse(categoryId, out id) ? (int?)id : null;
    }
    

提交回复
热议问题