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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - Laser.NET

WumiiRelatedItems C#创建一个线程为什么会消耗那么多的内存? 【跨平台的.NET解决方案】-Mono越来越成熟! [转]英语中符号以及标点符号的读法(或许对查看英文资料的人有些帮助) 06年中国互联网调查结果之:网民上网经常使用的网络服务 让短信更好的为企业服务!——短消息交互平台简介 GoF Patterns [ASP.NET2.0]一个方便使用的在线截图Web控件-WebImageMaker [转]经典的几个网络签名:) [新手点滴] IDbCommand.Prepare()方法。 [原创] 我的生活我安排,我的调度我做主!——介绍一个 日程安排/工作计划/任务调度 的Scheduling组件。(二) [原创] 我的生活我安排,我的调度我做主!——介绍一个 日程安排/工作计划/任务调度 的Scheduling组件。(一) [ADO.NET]由数据库触发器引发的问题 [转] IE的bug列表 [辅助工具] 一个方便将ASP代码升级到ASP.NET的小工具 -- ASP Code Migrator! 关于ASP.NET页面嵌入代码的高亮显示的问题。 [C#语言] C#语法中一个很奇怪的地方:) 这样的网站,2个月是否可能?? [新手点滴] C# vs VB.NET 哪个好?
[C#语言] C#语言中又一个自认为不太理想的地方。。。
Laser.NET · 2005-06-29 · via 博客园 - Laser.NET

与以往的C,C++相比,C#引入了foreach语句,确实让很多完成循环逻辑的代码简洁了不少。但是今天在写代码的时候却又让我感觉到C#在foreach语法上还有不够理想的地方:

翻阅了一下MSDN中的C# Programmer's Reference章节,关于foreach, in语句的定义如下:

foreach (type identifier in expression) statement

where:

type
The type of identifier.
identifier
The iteration variable that represents the collection element. If the iteration variable is a value type, it is effectively a read-only variable that cannot be modified.
expression
Object collection or array expression. The type of the collection element must be convertible to the identifier type. Do not use an expression that evaluates to null.

Evaluates to a type that implements IEnumerable or a type that declares a GetEnumerator method. In the latter case, GetEnumerator should either return a type that implements IEnumerator or declares all the methods defined in IEnumerator.

statement
The embedded statement(s) to be executed.

从语法上来看identifier前面必须要有type的声明,虽然这里的文档没有明确强调,但是事实上C#编译器会要求的,如果没有类型的声明将会报错。这就带来一个问题:就是foreach语句中用来枚举集合中元素的那个变量必须是一个新的变量声明,因为在表识符的前面必须要有类型。所以如果想要用前面已经声明的变量的话就不可能了。因此个人觉得这个是目前C#语言的一个缺陷,或者是不够合理的地方,不知道C#2.0有没有改进?

为什么我觉得不够合理?让我们再来看一下VB.NET中的for each语句的语法,那里就允许使用前面已经声明过的变量:

For Each element [ As datatype ] In group
   [ statements ]
[ Exit For ]
   [ statements ]
Next [ element ]

Parts

element
Required. Variable. Used to iterate through the elements of the collection. The data type of element must be such that the data type of the elements of group can be converted to it.
datatype
Required if element is not already declared. Data type of element. If element is declared outside this loop, you cannot use the As clause to redeclare it.
group
Required. Object variable. Must refer to an object collection or array.
statements
Optional. One or more statements between For Each and Next that are executed on each item in group.