A. Woods A. Woods - 1 year ago 76
C# Question

multiplying a variable without rewriting it

So I am fairly new to the "code world" and hopefully have a fairly simply question.

txtBox.Text = "x";

How would I make it so I am able to see 10 X's in my
without completely writing it out?

Answer Source

string has a constructor that repeats a character for a given number of times:

txtBox.Text = new string('x', 10);

It's documented here.

If what you want to repeat is a string (not a single character) you need a loop, but use a StringBuilder to minimise memory fragmentation. Try avoiding repetitive string concatenations:

var stringBuilder = new StringBuilder();
for(int i = 0; i < 10; i++)

txtBox.Text = stringBuilder.ToString();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download