Integer.TryParse - a better way?

后端 未结 7 1605
礼貌的吻别
礼貌的吻别 2020-12-08 06:51

I find myself often needing to use Integer.TryParse to test if a value is an integer. However, when you use TryParse, you have to pass a reference variable to the function,

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 07:32

    public static class Util {
    
        public static Int32? ParseInt32(this string text) {
            Int32 result;
            if(!Int32.TryParse(text, out result))
                return null;
            return result;
        }
    
        public static bool IsParseInt32(this string text) {
            return text.ParseInt32() != null;
        }
    
    }
    

提交回复
热议问题