MatchCollection.IEnumerable<Match>.GetEnumerator 方法
定义
返回一个循环访问集合的枚举器。Returns an enumerator that iterates through the collection.
virtual System::Collections::Generic::IEnumerator<System::Text::RegularExpressions::Match ^> ^ System.Collections.Generic.IEnumerable<System.Text.RegularExpressions.Match>.GetEnumerator() = System::Collections::Generic::IEnumerable<System::Text::RegularExpressions::Match ^>::GetEnumerator;
System.Collections.Generic.IEnumerator<System.Text.RegularExpressions.Match> IEnumerable<Match>.GetEnumerator ();
abstract member System.Collections.Generic.IEnumerable<System.Text.RegularExpressions.Match>.GetEnumerator : unit -> System.Collections.Generic.IEnumerator<System.Text.RegularExpressions.Match>
override this.System.Collections.Generic.IEnumerable<System.Text.RegularExpressions.Match>.GetEnumerator : unit -> System.Collections.Generic.IEnumerator<System.Text.RegularExpressions.Match>
Function GetEnumerator () As IEnumerator(Of Match) Implements IEnumerable(Of Match).GetEnumerator
返回
用于循环访问集合的枚举数。An enumerator that can be used to iterate through the collection.
实现
例外
该集合是只读的。The collection is read-only.
注解
返回的 IEnumerator<T> 功能通过公开属性来循环访问集合 Current 。您可以使用枚举器读取集合中的数据,但不能修改该集合。The returned IEnumerator<T> provides the ability to iterate through the collection by exposing a Current property .You can use enumerators to read the data in a collection, but not to modify the collection.
最初,枚举数定位在集合中第一个元素的前面。Initially, the enumerator is positioned before the first element in the collection. 在此位置上,未定义 Current。At this position, Current is undefined. 因此,在读取的值之前,必须调用 MoveNext 方法,以将枚举器前进到集合的第一个元素 Current 。Therefore, you must call the MoveNext method to advance the enumerator to the first element of the collection before reading the value of Current.
Current 返回相同的对象,直到 MoveNext 再次调用以 MoveNext 将设置为 Current 下一个元素。Current returns the same object until MoveNext is called again as MoveNext sets Current to the next element.
如果 MoveNext 越过集合的末尾,则枚举器将定位在集合中最后一个元素之后,并 MoveNext 返回 false 。If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false. 当枚举器位于此位置时,对的后续调用 MoveNext 也将返回 false 。When the enumerator is at this position, subsequent calls to MoveNext also return false. 如果最后一次调用 MoveNext 返回 false , Current 则未定义。If the last call to MoveNext returned false, Current is undefined. 无法再次将 Current 设置为集合的第一个元素;必须改为创建新的枚举器实例。You cannot set Current to the first element of the collection again; you must create a new enumerator instance instead. 如果对集合所做的更改(如添加、修改或删除元素),则枚举器的行为是不确定的。If changes are made to the collection, such as adding, modifying, or deleting elements, the behavior of the enumerator is undefined.
枚举器没有对集合的独占访问权限,因此只要集合保持不变,枚举数就会保持有效。An enumerator does not have exclusive access to the collection so an enumerator remains valid as long as the collection remains unchanged. 如果对集合进行了更改(如添加、修改或删除元素),则枚举器会失效,你可能会收到意外的结果。If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is invalidated and you may get unexpected results. 此外,枚举集合不是线程安全的过程。Also, enumerating a collection is not a thread-safe procedure. 若要确保线程安全,应在枚举器期间锁定集合,或在集合上实现同步。To guarantee thread-safety, you should lock the collection during enumerator or implement synchronization on the collection.
命名空间中集合的默认实现 System.Collections.Generic 不会同步。Default implementations of collections in the System.Collections.Generic namespace aren't synchronized.