Type Summary

						public sealed class CharEnumerator : IEnumerator,
                                   ICloneable
{
  // Properties
     public char Current { get; }

  // Methods
     public object Clone ();
     public bool MoveNext ();
     public void Reset ();

  // Explicit Interface Members
     object IEnumerator.Current { get; }
}

BA Our reason for adding this class to the BCL was not that we felt there was a huge demand for enumerating characters. It was required to enable efficient support for applying foreach over strings. Simply using IEnumerator would have caused a boxing operation for each character accessed out of the string.

string s = "John Smith";
foreach (char c in  s) {
   Console.WriteLine ("{0}-", c);
}

A side note on this is that the C# compiler now does not even use this class. It special-cases ...

Get .NET Framework Standard Library Annotated Reference, Volume 1: Base Class Library and Extended Numerics Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.