惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
B
Blog
罗磊的独立博客
宝玉的分享
宝玉的分享
Vercel News
Vercel News
Martin Fowler
Martin Fowler
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
T
Threatpost
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
I
Intezer
P
Privacy International News Feed
D
Docker
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
Lohrmann on Cybersecurity
Google DeepMind News
Google DeepMind News
The Last Watchdog
The Last Watchdog
A
Arctic Wolf
IT之家
IT之家
S
SegmentFault 最新的问题
S
Securelist
博客园 - 叶小钗
N
News and Events Feed by Topic
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - Franky
GbyAI
GbyAI
AI
AI
Y
Y Combinator Blog
WordPress大学
WordPress大学
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
N
News | PayPal Newsroom
The Cloudflare Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
I
InfoQ

博客园 - Adrian H.

Hello in 2025 Hello World 为Silverlight控件添加鼠标滚轮支持 - Adrian H. - 博客园 jQuery太好使了 一次性下载.NET源码和pdb,并在VS2005中进行调试 Windows Server 2008: Server Core初体验 - Adrian H. C# Future Focus: 动态查找(Dynamic Lookup) Volta : Democratizing the Cloud, 1st CTP Released! Parallel Extensions for .NET 3.5 CTP! Silverlight 2.0, .NET类库源码, ASP.NET MVC... VS 2008与老版本插件的兼容性问题导致Crash Silverlight Tools for VS 2008 RTM [C# 3.0] 传递匿名类型对象的问题 Visual Studio 2008 RTM! 在 .NET Framework x (x < 3.5) 环境下运行带有扩展方法(Extension Method)的程序 使用ActionScript 3中的反射,实现单元测试TestRunner ASP.NET MVC Framework Announced .NET Framework 源代码将于今年发布 N-Tiers?
LINQ表达式中关于显式范围变量的Bug
Adrian H. · 2008-02-16 · via 博客园 - Adrian H.

MSDN博客中的一篇文章提到了.NET 3.5 SP1会带来的处个修正,见以下代码:

var floats = new ArrayList { 2.5f, 3.5f, 4.5f };
var ints = from int i in floats select i;

注意from后面声明的显式类型(int)。开发者会很自然地认为ints为[2,3,4],但在现在版本.NET 3.5环境下得到的却是[2,4,4]。

原来那段LINQ表达式将会被编译为以下方法调用:

var ints = floats.Cast<int>().Select<int,int>(i => i);

问题便出现在Cast这个扩展方法中,而Cast内部是通过调用Convert类的ToInt32方法转换ArrayList中元素类型的,而这个方法既不是对浮点数进行截去小数,也不是简单的round: ToInt32方法的文档中是这样描述返回值的:

value rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6.

这种数值转换的方式被称为Banker's rounding

这是个程序语义的问题,将在以后的.NET Frameworkk 3.5 SP1中解决,估计只是修改Cast方法的实现。现在的应对方法是在一些应用场景中不使用显式范围变量。