的foreach [英] foreach

查看:88
本文介绍了的foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。任何人都可以评论以下技术的效率:


foreach(SomeNtemDotNet.Collection中的SomeItem项目)

{

/ /无论什么

}


我关心的是收藏品可以在每个
迭代上调用属性本身,而不是在foreach开始时仅调用一次。如果

那么就没有办法知道检索这个属性的调用是否是
a耗时的操作(该属性可能不会存储为简单的

成员变量)。如果是这样,那么事情应该改变如下:


收集MyCollection = SomeNativeDotNet.Collection;

foreach(MyCollection中的SomeItem项目)

{

//无论什么

}


任何人都可以提供任何见解。谢谢。

解决方案

据我了解,SomeNativeDotNet.Collection.GetEnumerator()将被评估一次,然后再计算
,然后所有后续访问都是在返回的

枚举器上(它可以直接访问返回的

集合);所以不,这不应该打电话。一遍又一遍地收集...


Marc


迈克尔,

它只执行一次。您可以在ildasm中打开程序集以

验证。


Brian


Michael Brown写道:

你好。任何人都可以评论以下技术的效率:

foreach(SomeNativeDotNet.Collection中的SomeItem项目)
//
//
}
<我担心的是收集。可以在每次迭代时调用属性本身,而不是在foreach开始时仅调用一次。如果
那么就没有办法知道检索这个属性的调用是否是一个耗时的操作(该属性可能不会存储为一个简单的成员变量)。如果是这样,那么事情应该改变如下:

收集MyCollection = SomeNativeDotNet.Collection;
foreach(MyCollection中的SomeItem项目)
//
// />}

任何人都可以提供任何见解。谢谢。




>据我了解,SomeNativeDotNet.Collection.GetEnumerator()将

评估一次,然后所有后续访问都返回到返回的
枚举器(它将拥有自己的直接访问权限)
收藏);所以不,这不应该打电话。一遍又一遍地收集......




谢谢。我查看了C#规范,这显然是正确的。


Hi there. Can anybody comment on the efficiency of the following technique:

foreach (SomeItem Item in SomeNativeDotNet.Collection)
{
// Whatever
}

My concern is that the "Collection" property itself may be invoked on each
iteration rather than being called just once at the outset of "foreach". If
so then there''s no way to know whether the call to retrieve this property is
a time-consuming operation (the property may not be stored as a simple
member variable). If so then things should be changed as follows:

Collection MyCollection = SomeNativeDotNet.Collection;
foreach (SomeItem Item in MyCollection)
{
// Whatever
}

Can anyone offer any insight. Thanks.

解决方案

As I understand it, SomeNativeDotNet.Collection.GetEnumerator() will be
evaluated once, and then all subsequent access is onto the returned
enumerator (which will have its own direct access to the returned
collection); so no, this shouldn''t call .Collection over and over...

Marc


Michael,

It''s only executed once. You can open the assembly in ildasm to
verify.

Brian

Michael Brown wrote:

Hi there. Can anybody comment on the efficiency of the following technique:

foreach (SomeItem Item in SomeNativeDotNet.Collection)
{
// Whatever
}

My concern is that the "Collection" property itself may be invoked on each
iteration rather than being called just once at the outset of "foreach". If
so then there''s no way to know whether the call to retrieve this property is
a time-consuming operation (the property may not be stored as a simple
member variable). If so then things should be changed as follows:

Collection MyCollection = SomeNativeDotNet.Collection;
foreach (SomeItem Item in MyCollection)
{
// Whatever
}

Can anyone offer any insight. Thanks.




> As I understand it, SomeNativeDotNet.Collection.GetEnumerator() will be

evaluated once, and then all subsequent access is onto the returned
enumerator (which will have its own direct access to the returned
collection); so no, this shouldn''t call .Collection over and over...



Thanks. I looked into the C# specification and this is apparently correct.


这篇关于的foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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