int.TryParse = null if not numeric?

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

    Int is a value type which means there is no such thing as a null int. So no, TryParse will never alter the out parameter so that it is null.

    But the problem you're having is you're passing a string to the out parameter of TryParse when its expecting an integer.

    You need something like this...

    Int categoryID = 0;
    string strCategoryID = "somestringmaybeitsaninteger";
    
    int.TryParse(strCategoryID, out categoryID);
    

提交回复
热议问题