Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TextElementEnumerator Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Enumerates the text elements of a string.

Namespace:   System.Globalization
Assembly:  mscorlib (in mscorlib.dll)


public class TextElementEnumerator : IEnumerator


Gets the current text element in the string.


Gets the index of the text element that the enumerator is currently positioned over.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the current text element in the string.


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Advances the enumerator to the next text element of the string.


Sets the enumerator to its initial position, which is before the first text element in the string.


Returns a string that represents the current object.(Inherited from Object.)

The .NET Framework defines a text element as a unit of text that is displayed as a single character, that is, a grapheme. A text element can be any of the following:

  • A base character, which is represented as a single Char value. For example, LATIN CAPITAL LETTER A (U+0041) and LATIN SMALL LETTER AE (U+00E6) are base characters.

  • A combining character sequence, which consists of a base character and one or more combining characters. For example, example, LATIN CAPITAL LETTER A (U+0041) followed by COMBINING MACRON (U+0304) is a combining character sequence.

  • Surrogate pairs, which the Unicode Standard defines as a coded character representation for a single abstract character that consists of a sequence of two code units: a high surrogate, and a low surrogate. Surrogate pairs are used to represent characters outside of the Unicode Basic Multilingual Plane as UTF-8 encoded characters. For example, GOTHIC LETTER SAUIL (U+10343) is represented in UTF-8 encoding as a high surrogate whose value is 0xD800 and a low surrogate whose value is 0xDF43. A surrogate pair can represent a base character or a combining character.

The TextElementEnumerator class allows you to work with the text elements in a string rather than with single Char objects.

You instantiate a TextElementEnumerator object that represents a particular string by passing the string to the StringInfo.GetTextElementEnumerator method. This returns an enumerator that is positioned before the first text element in the string. Calling the Reset method also brings the enumerator back to this position. Because this represents an invalid state, you must call MoveNext to advance the enumerator to the first text element of the string before reading the value of the Current property to return the current text element.

When working with a TextElementEnumerator object, you are responsible for positioning the enumerator. The Current property returns the same text element until you call either MoveNext or Reset. The enumerator is in an invalid state if it is positioned before the first text element or after the last text element in the string. When the enumerator is in an invalid state, attempting to retrieve the value of the Current property throws an exception. You can determine whether the enumerator is in an invalid state by testing whether the return value of the MoveNext property is false.

The TextElementEnumerator object represents a snapshot of the current state of a string variable or string literal at the moment that the TextElementEnumerator object is instantiated. Note that:

  • Text element enumerators can only be used to read data in a string. They cannot modify the underlying string.

  • An enumerator does not have exclusive access to the string that it represents. A string variable can be modified after the enumerator is created.

  • A TextElementEnumerator object enumerates the text elements present in the string at the time that the TextElementEnumerator object was instantiated. It does not reflect any subsequent changes to the string variable if that variable is modified afterward.

  • Because the TextElementEnumerator class does not override Object.Equals, two TextElementEnumerator objects that represent the same string will be considered unequal.

The following example uses the TextElementEnumerator class to enumerate the text elements of a string.

using System;
using System.Globalization;

public class SamplesTextElementEnumerator  {

   public static void Main()  {

      // Creates and initializes a String containing the following:
      //   - a surrogate pair (high surrogate U+D800 and low surrogate U+DC00)
      //   - a combining character sequence (the Latin small letter "a" followed by the combining grave accent)
      //   - a base character (the ligature "")
      String myString = "\uD800\uDC00\u0061\u0300\u00C6";

      // Creates and initializes a TextElementEnumerator for myString.
      TextElementEnumerator myTEE = StringInfo.GetTextElementEnumerator( myString );

      // Displays the values returned by ElementIndex, Current and GetTextElement.
      // Current and GetTextElement return a string containing the entire text element. 
      Console.WriteLine( "Index\tCurrent\tGetTextElement" );
      while (myTEE.MoveNext())  {
         Console.WriteLine( "[{0}]:\t{1}\t{2}", myTEE.ElementIndex, myTEE.Current, myTEE.GetTextElement() );



This code produces the following output.  The question marks take the place of high and low surrogates.

Index   Current GetTextElement
[0]:    ??      ??
[2]:    a`      a`
[4]:    Æ       Æ


Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

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

Return to top
© 2018 Microsoft