Home
C#
CharEnumerator
This page was last reviewed on Jun 28, 2023.
Dot Net Perls
CharEnumerator. This C# type is used to loop over a string. The CharEnumerator is acquired through the GetEnumerator method on the string type.
GetEnumerator
Type info. CharEnumerator offers few advantages over other constructs. But it can be used when an interface (IEnumerator) is required by a method.
char
Example program. This program declares a string literal and then calls the GetEnumerator method. It shows the usage in C# of CharEnumerator.
Then We use a while-loop to repeatedly call the MoveNext method. The Current property returns the current character.
String Literal
using System; class Program { static void Main() { // Input string. string val = "dotnet"; // Get enumerator. CharEnumerator e = val.GetEnumerator(); // Use in loop. while (e.MoveNext()) { char c = e.Current; Console.WriteLine(c); } } }
d o t n e t
Performance. You can loop through strings with foreach and for-loops. I wondered if the CharEnumerator version of the string loop had any advantage over these methods.
Loop, String Chars
Version 1 This benchmark tests a loop over a short four-character string using the CharEnumerator-style code.
Version 2 This version of the code uses a loop over the string as well, but uses the foreach-style code.
Result The CharEnumerator method of looping overall the characters in a string is much slower than the foreach method.
using System; using System.Diagnostics; class Program { const int _max = 100000000; static void Main() { var s1 = Stopwatch.StartNew(); // Version 1: use CharEnumerator. for (int i = 0; i < _max; i++) { Method1("test"); } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use foreach-loop. for (int i = 0; i < _max; i++) { Method2("test"); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000 * 1000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000 * 1000) / _max).ToString("0.00 ns")); } static int Method1(string val) { int res = 0; CharEnumerator c = val.GetEnumerator(); while (c.MoveNext()) { res += (int)c.Current; } return res; } static int Method2(string val) { int res = 0; foreach (char c in val) { res += (int)c; } return res; } }
33.33 ns CharEnumerator 4.79 ns foreach
Discussion. Using CharEnumerator directly is not usually worthwhile. But if you have a reference of type IEnumerator, you can use CharEnumerator through those types.
Note CharEnumerator implements those interfaces. But this is not something that will occur in many programs.
Summary. The CharEnumerator type presents an implementation of an enumerator for the string type. To get a CharEnumerator, call GetEnumerator on a string instance.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 28, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.