Share via


MatchCollection Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Represents the set of successful matches found by iteratively applying a regular expression pattern to the input string.

Inheritance Hierarchy

System. . :: . .Object
  System.Text.RegularExpressions..::..MatchCollection

Namespace:  System.Text.RegularExpressions
Assembly:  System.Text.RegularExpressions (in System.Text.RegularExpressions.dll)

Syntax

'Declaration
<SerializableAttribute> _
Public Class MatchCollection _
    Implements ICollection, IEnumerable
[SerializableAttribute]
public class MatchCollection : ICollection, 
    IEnumerable
[SerializableAttribute]
public ref class MatchCollection : ICollection, 
    IEnumerable
[<SerializableAttribute>]
type MatchCollection =  
    class
        interface ICollection
        interface IEnumerable
    end
public class MatchCollection implements ICollection, IEnumerable

The MatchCollection type exposes the following members.

Properties

  Name Description
Public property Count Gets the number of matches.
Public property IsReadOnly Gets a value that indicates whether the collection is read only.
Public property IsSynchronized Gets a value indicating whether access to the collection is synchronized (thread-safe).
Public property Item Gets an individual member of the collection.
Public property SyncRoot Gets an object that can be used to synchronize access to the collection.

Top

Methods

  Name Description
Public method CopyTo Copies all the elements of the collection to the given array starting at the given index.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetEnumerator Provides an enumerator that iterates through the collection.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

The collection is immutable (read-only) and has no public constructor. The Regex..::..Matches method returns a MatchCollection object.

The collection contains zero or more System.Text.RegularExpressions..::..Match objects. If the match is successful, the collection is populated with one System.Text.RegularExpressions..::..Match object for each match found in the input string. If the match is unsuccessful, the collection contains no System.Text.RegularExpressions..::..Match objects, and its Count property equals zero.

When applying a regular expression pattern to a particular input string, the regular expression engine uses either of two techniques to build the MatchCollection object:

  • Direct evaluation.

    The MatchCollection object is populated all at once, with all matches resulting from a particular call to the Regex..::..Matches method. This technique is used when the collection's Count property is accessed. It typically is the more expensive method of populating the collection and entails a greater performance hit.

  • Lazy evaluation.

    The MatchCollection object is populated as needed on a match-by-match basis. It is equivalent to the regular expression engine calling the Regex..::..Match method repeatedly and adding each match to the collection. This technique is used when the collection is accessed through its GetEnumerator method, or when it is accessed using the foreach statement (in C#) or the For Each...Next statement (in Visual Basic).

To iterate through the members of the collection, you should use the collection iteration construct provided by your language (such as foreach in C# and For Each…Next in Visual Basic) instead of retrieving the enumerator that is returned by the GetEnumerator method.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.Text.RegularExpressions Namespace