MatchCollection.GetEnumerator Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Provides an enumerator that iterates through the collection.
public:
virtual System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator
Returns
An object that contains all Match objects within the MatchCollection.
Implements
Exceptions
A time-out occurred.
Remarks
Instead of calling the GetEnumerator() method to retrieve an enumerator that lets you iterate through the Match objects in the collection, you should use the collection iteration construct provided by your programming language (such as foreach in C#).
Iterating the members of the MatchCollection using the GetEnumerator() method (or the foreach statement) causes the regular expression engine to populate the collection on an as-needed basis using lazy evaluation. In contrast, the regular expression engine uses direct evaluation to populate the collection all at once when the Count property is accessed. This can be a much more expensive method of building the collection than lazy evaluation.
Because the MatchCollection object is generally populated by using lazy evaluation, trying to navigate to the next member of the collection may throw a 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.