int.TryParse = null if not numeric?

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

    ** this answer was down-voted a lot ** Although it is a possible solution - it is a bad one performance wise, and probably not a good programming choice.

    I will not delete it, as I guess many programmers might not be aware of this, so here is an example how not to do things:

    use try and catch

    try
    {
    res = Int32.Parse(strVAR)
    }
    catch(exception ex) 
    {
     return null;
    }
    

提交回复
热议问题