简体   繁体   English

这是一个什么样的for循环?

[英]What kind of for loop is this?

Is there a special name for this type of for loop in c#?在 C# 中,这种类型的 for 循环是否有特殊名称?

for (EventRecord eventdetail = logReader.ReadEvent(); eventdetail != null; eventdetail = logReader.ReadEvent())

EDIT: I'm trying to research if there is a way to do this in a multithreaded way.编辑:我正在尝试研究是否有办法以多线程方式执行此操作。 since Parallel.For can't be called in the same fashion I was hoping there was a special name for the above type of for loop since it isn't incremented or decremented like the typical for loop is.由于Parallel.For不能以相同的方式调用,我希望上述类型的for loop有一个特殊的名称,因为它不像典型的for loop那样递增或递减。

It reads an EventRecord with the first logReader.ReadEvent,它使用第一个 logReader.ReadEvent 读取一个 EventRecord,

for(EventRecord eventdetail = logReader.ReadEvent(); ....

then checks if this instance is null,然后检查此实例是否为空,

for(.....;eventdetail != null;....)

then executes the body of the for loop and then ask again for another EventRecord.然后执行 for 循环的主体,然后再次请求另一个 EventRecord。

for(.....;.....;eventdetail = logReader.ReadEvent())

and again checks if the return is null.并再次检查返回是否为空。 This continues until the EventRecord returned by ReadEvent is null.这一直持续到 ReadEvent 返回的 EventRecord 为空。

It is smart but I would prefer a more readable while loop它很聪明,但我更喜欢更易读的 while 循环

while((EventRecord eventdetail = logReader.ReadEvent()) != null)
{
   .....
}

There is no special name.没有特别的名字。 It just an ordinary run of the mill for loop .它只是普通的for loop运行。 It has an initializatin-clause,condition-clause,iteration-clause.它有一个初始化子句,条件子句,迭代子句。

For what its worth...any on of the clause can be omitted!对于它的价值......任何条款都可以省略!
IE For(;;) will create an infinite loop. IE For(;;)将创建一个无限循环。

for statement (C# reference) for 语句(C# 参考)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM