for 和 foreach 有什么区别? [英] What is the difference between for and foreach?
本文介绍了for 和 foreach 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
for
和 foreach
循环之间的主要区别是什么?
在哪些情况下我们可以使用 for
而不是 foreach
,反之亦然.
是否可以用一个简单的程序来展示?
在我看来两者都一样.我无法区分它们.
解决方案
for
循环是一种结构,表示执行此操作 n. 次".>
foreach
循环是一种结构,表示对这个 IEnumerable 中的 每个 值/对象执行此操作"
What is the major difference between for
and foreach
loops?
In which scenarios can we use for
and not foreach
and vice versa.
Would it be possible to show with a simple program?
Both seem the same to me. I can't differentiate them.
解决方案
a for
loop is a construct that says "perform this operation n. times".
a foreach
loop is a construct that says "perform this operation against each value/object in this IEnumerable"
这篇关于for 和 foreach 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文