Hy, I have a int[] x with 10000 indexes, and I`m using a code like this to put every value in a newline in a textbox, but my code will take atleast a couple minuts to fill the textbox, is there a quickier way to do the same?
for ( int x = 0; X < 10000; x++)
{
textBox1.Text += randomNumber[x] + Environment.NewLine;
}
Yes, use a string builder for things like this:
StringBuilder builder = new StringBuilder(10000);
for (int x = 0; x < 10000; x++)
{
builder.AppendLine(randomNumber[x]);
}
textBox1.Text = builder.ToString();
Otherwise you are 'coughing up a string', a common pitfall to new programmers. Here is a great blog post from Joel Spolsky describing common pitfalls with strings and immutability.
It's not clear whether randomNumber collection size is 10000 or not, but if it is, you can use String.Join method:
textBox1.Text = string.Join(Environment.NewLine, randomNumber);
It will use StringBuilder internally anyway, but is better to read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With