Reputation: 21
For each problem
char[] charArray = zig.ToCharArray(); //puts each character in zigs in an array
Array.Reverse(charArray); // Reverses the array
foreach (char zigArray in charArray)
{
Console.Write(zigArray);
}
Console.ReadLine();
This block works how i want it to but i don't understand why i need a for each statement here. I am confused why i shouldn't just do console.write after i have reversed the string.
(I am following a tutorial)
Upvotes: 0
Views: 44
Reputation: 156918
The simple answer is: try it!
I did. The answer: there is none. The end result on the screen is the same, however, a minor detail, the reversed string is now a character array which makes it a little harder to see what happens right away.
Calling Console.Write
on an array of chars outputs the text as expected and reproduces the exact samen result as foreach
does (and not System.Array
or something like that you might see when printing an object
).
Why did they use foreach
then? Probably to learn you how iterating over a collection works. I guess it is just a bad example, move on.
Upvotes: 1