System.Text.RegularExpressions.MatchCollection.GetEnumerator Method

Provides an enumerator that iterates through the collection.

Syntax

public IEnumerator GetEnumerator ()

Returns

An object that contains all System.Text.RegularExpressions.Match objects within the System.Text.RegularExpressions.MatchCollection.

Remarks

Instead of calling the MatchCollection.GetEnumerator method to retrieve an enumerator that lets you iterate through the System.Text.RegularExpressions.Match objects in the collection, you should use the group iteration construct (such as foreach in C# and For Each…Next in Visual Basic) provided by your programming language.

Iterating the members of the System.Text.RegularExpressions.MatchCollection object using the MatchCollection.GetEnumerator method (or the foreach statement in C# and the For Each...Next statement in Visual Basic) causes the regular expression engine to populate the collection on an as needed basis using lazy evaluation. This is analogous to repeatedly calling the Regex.Match(string) method, and then adding the resulting match to the System.Text.RegularExpressions.MatchCollection object. In contrast, the regular expression engine uses direct evaluation to populate the collection all at once when the MatchCollection.Count property is accessed. This can be a much more expensive method of building the collection than lazy evaluation.

Because the System.Text.RegularExpressions.MatchCollection object is generally populated by using lazy evaluation, trying to navigate to the next member of the collection may throw a System.Text.RegularExpressions.RegexMatchTimeoutException exception. This exception can be thrown if a time-out value for matching operations is in effect, and the attempt to find the next match exceeds that time-out interval.

Requirements

Namespace: System.Text.RegularExpressions
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0