int.TryParse = null if not numeric?

前端 未结 8 1516
太阳男子
太阳男子 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条回答
  •  Happy的楠姐
    2021-02-19 11:32

    Do you want to do something like this?

    public int? Parse(string categoryID) 
    {
      int value;
      if (int.TryParse(categoryID, out value))
      {
        return value;
      }
      else
      {
        return null;
      }
    }
    

提交回复
热议问题