Request.Browser.Version可能不是双精度型

Avoid writing code like this to do browser detection:

避免编写这样的代码来进行浏览器检测:

if(Double.Parse(Request.Browser.Version) < 5.2) { // 5 or less

if(Double.Parse(Request.Browser.Version)<5.2){// 5以下

Why might this be a problem? Well, Request.Browser.Version is typed in the Base Class Library as a string. It's a string for a reason - because it may not necessarily be a double. When one writes Double.Parse(someString) they're basically saying that the folks who chose the type for Request.Browser.Version were mistaken, and that a double would have been a better chose.

为什么会出现问题? 好吧,Request.Browser.Version在基类库中作为字符串键入。 这是一个字符串,这是有原因的-因为它不一定是双精度型。 当人们写Double.Parse(someString)时,他们基本上是在说为Request.Browser.Version选择类型的人是错误的,而双精度会更好。

This can be a problem when a browser like IE7.0 beta comes out and the value of Request.Browser.Version is "7.0b." This also applies to Mac's Safari and its User agent version which changed recently from 2.0 to 2.0.1 or 2.0.2.

当出现类似IE7.0 beta的浏览器并且Request.Browser.Version的值为“ 7.0b”时,这可能是一个问题。 这也适用于Mac的Safari及其用户代理版本,该版本最近从2.0更改为2.0.1或2.0.2

Exception: Input string was not in a correct format.
Exception: System.FormatException
Message: Input string was not in a correct format.
Source: mscorlib
at System.Number.ParseDouble(String s, NumberStyles style, NumberFormatInfo info)
at System.Double.Parse(String s, NumberStyles style, IFormatProvider provider)
at System.Double.Parse(String s)

例外:输入字符串的格式不正确。 异常:System.FormatException 消息:输入字符串的格式不正确。 资料来源:mscorlib 在System.Number.ParseDouble(String s,NumberStyles样式,NumberFormatInfo信息) 在System.Double.Parse(String s,NumberStyles样式,IFormatProvider提供程序) 在System.Double.Parse(String s)

Unexpected exceptions tend to cramp one's style. I like the philosophy that "if you ever get an unexpected exception, you didn't test your code well enough." Why? Because clearly it wasn't anticipated, otherwise there'd have been code to handle it. This line (and the code around it you can't see) never expected a non-double to be in Request.Browser.Version, but the property's string type was the first tip that other things could happen.

意外的例外往往会束缚自己的风格。 我喜欢这样的理念:“如果遇到意外的异常,则说明您对代码的测试不够好。” 为什么? 因为显然这不是预期的,否则会有代码来处理它。 此行(以及您周围看不到的代码)从未期望Request.Browser.Version中存在非双精度型,但是该属性的字符串类型是可能发生其他事情的第一个提示。

翻译自: https://www.hanselman.com/blog/requestbrowserversion-may-not-be-a-double

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值