With a foreach statement, you can visit each element of a collection. For example, the above for statement:
for (int tIndex = 0; tIndex<pValues.Length; tIndex++)
{
if (pValues[tIndex]==pTestValue)
...
foreach (int tValueFound in pValues)
{
if (tValueFound==pTestValue)
...
A foreach statement can be used for any type that implements the IEnumerable interface:
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
In Java, the equivalent method to GetEnumerator is called iterator. The IEnumerator interface is:
public interface IEnumerator
{
bool MoveNext();
object Current{ get; }
void Reset();
}
In Java, the equivalent interface to IEnumerator is called Iterator.
You can also produce your own types that implement this interface.