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

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

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

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

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