I don’t consider myself to be the greatest developer in the world, but I thought I could at least loop over a list of strings!
Here’s my function:
public string liststrings() {
List<string> strings = new List<string>();
strings.Add("First");
strings.Add("Second");
strings.Add("Third");
string output = string.Empty;
for (int i = 0; i < strings.Count(); i++ )
{
output += output + strings[i] + "<br />";
}
return output;
}
This function returns the following html:
First<br />
First<br />
Second<br />
First<br />
First<br />
Second<br />
Third<br />
Where are the extra iterations coming from?
FYI: I come from a primarily VB script background and I can do this with an array in VB script without a problem. What different about lists or C# syntax that’s fouling this up?
Thanks for any help.
You’re adding output twice.
is equivalent to:
You could use:
A better option might be a
StringBuilder.