for-in loop

Top  Previous  Next

What is translated > Statements > for loop's > for-in loop

A Delphi for-in loop iterates over the elements of a string, array, set, collection, or another enumerable type.

 

Its general syntax is:

 

var

  A: TypeName;

begin

  for A in B do

    DoSomething(A);

end;

 

Depending on the type of B, the variable A can represent:

 

A character in a string
An element of an array
A member of a set
An element of a collection
A value supplied by a custom enumerator

 

These loops are normally translated into C# foreach statements:

 

TypeName A;

 

foreach (TypeName element_0 in B)

{

    A = element_0;

    DoSomething(A);

}

 

The temporary variable element_0 is the iteration variable of the C# foreach statement. Its value is assigned to the original Delphi variable A at the beginning of each iteration.

 

C# arrays and strings provide the required enumeration support directly. Other translated types must implement IEnumerable, IEnumerable<T>, or the corresponding C# enumeration pattern using GetEnumerator(), MoveNext(), and Current.



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content