Posted by: Cirilo Meggiolaro | 02/11/2009

Tip of the day #120 – CharEnumerator class

The CharEnumerator class has been designed to help iterating over characters in a string. Actually, the GetEnumerator method from the string object returns a CharEnumerator.

It’s important to say that the CharEnumerator is read-only and provides some methods to help the navigation.

Properties and methods

  • char Current: Read-only property that retrieves the char existing in the current position within the string;
  • object Clone(): Method that retrieves a copy of the full CharEnumerator object;
  • bool MoveNext(): Moves the cursor to the next character within the CharEnumerator object;
  • void Reset(): Moves the cursor to the position before the first character within the CharEnumerator object.

How to…

The following code snippet uses the MoveNext method and Current property to loop through all characters within a string and display it.

string s = “Tip of the day #120”;
CharEnumerator c = s.GetEnumerator();

while (c.MoveNext())


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: