在c#编程过程中,decimal.tryparse方法和decimal.parse方法都可以将字符串string转换为decimal类型,但两者还是有区别,最重要的区别在于decimal.tryparse方法在字符串无法转换为decimal类型的情况下不会引发程序异常,而decimal.parse方法则是直接抛出程序异常。decimal.tryparse方法在无法转换的情况下返回false,并且使用了out参数进行转换。

decimal.tryparse方法的签名为:static bool tryparse(string s, out decimal result)

decimal.parse方法的签名为:static decimal parse(string s)

使用decimal.tryparse方法进行类型转换的时候,decimal变量无需先行赋值,在调用decimal.tryparse方法后,将自动带出结果值。

备注:原文转载自博主个人站it技术小趣屋,原文链接为c#中decimal.tryparse方法和decimal.parse方法的异同之处_it技术小趣屋。