【转载】C#中double.TryParse方法和double.Parse方法的异同之处

时间:2023-03-08 23:18:53
【转载】C#中double.TryParse方法和double.Parse方法的异同之处

在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技术小趣屋

博主个人技术交流群:960640092,博主微信公众号如下:

【转载】C#中double.TryParse方法和double.Parse方法的异同之处