C# foreach string code example
Example 1: c# foreach
var numbers = new NumberList<int> { 0, 1, 2, 3, 4, 5 };
foreach (int num in numbers)
{
DoSomething();
}
Example 2: c# iterate over string
// Regular Strings
string foo = "hello world", bar = string.Empty;
foreach(char c in foo){
bar += c;
}
// StringBuilder
string foo = "hello world";
StringBuilder bar = new StringBuilder();
foreach (char c in foo)
{
bar.Append(c);
}