为什么你应该避免遍历使用的foreach不可变的值类型的集合? [英] Why should you avoid to iterate over immutable value-type collections using foreach?

查看:131
本文介绍了为什么你应该避免遍历使用的foreach不可变的值类型的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个编码标准文档,我发现这样一句话:

In a coding standards document, I found this statement:

避免使用的foreach遍历   不可变的值类型的集合。   例如。字符串数组。

Avoid using foreach to iterate over immutable value-type collections. E.g. String arrays.

为什么要这样避免?

推荐答案

您不应该回避它。您正在阅读的编码标准文件是在胡说八道。试图找到作者,并要求他解释。

You shouldn't avoid it. The coding standard document you're reading is talking nonsense. Try to find the author and ask him to explain.

除了别的,字符串是一个引用类型和数组始终是可变的......这让我关心文档的其余部分的质量,是诚实。是否还有其他可疑的建议?

Aside from anything else, string is a reference type and arrays are always mutable... this makes me concerned about the quality of the rest of the document, to be honest. Are there any other suspicious recommendations?

(这有可能是不可改变的,是为了参考值类型,而不是集合 - 的事实,这是不明确的是另一种令人担忧的迹象,IMO)

(It's possible that "immutable" was meant to refer to the value type rather than the collection - the fact that it's ambiguous is another worrying sign, IMO.)

这篇关于为什么你应该避免遍历使用的foreach不可变的值类型的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆