I was learning about Collections, in this case the interfaces IEnumerable and IEnumerator, I studied that with this simple example:
string s = "Hello world";
IEnumerator rator = s.GetEnumerator();
while (rator.MoveNext ())
{
char c = (char)rator.Current;
Console.WriteLine(c+".");
}
But then I thought... I can do this with foreach loop, so I tried:
foreach (char c in s)
{
Console.WriteLine(c+".");
}
If I can do the same with the foreach loop, Why Do I have to use the interfaces IEnumerable and IEnumerator? What is better in performance? Imagine, I am doing a program which spell each letter of a big text, What Would I have to use? Interfaces or foreach loop?
I have been researching about foreach loops and I found that foreach works with interfaces.