Length属性填充相应的textBox3并正确更新值,因为字符串是从textBox1追加的。 textBox4只读取16,初始StringBuilder值(默认值),它在第17个字符处不会更改为32(在第33个字符处也不会更改为64)。
为什么容量属性不会继续填充/更新textBox4?
public partial class StringBuilderExercise : Form
{
public StringBuilderExercise()
{
InitializeComponent();
}
StringBuilder buffer = new StringBuilder();
// input textbox
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
// button appends textBox1 string to textBox2
private void append_Click(object sender, EventArgs e)
{
textBox2.AppendText(textBox1.Text);
textBox3.Text = textBox2.Text.Length.ToString();
textBox4.Text = buffer.Capacity.ToString();
textBox1.Clear();
}
//read-only textbox to display string append concat
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
//read-only textbox to display length of string
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
//read-only textbox to display capacity of string buffer
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
}
答案 0 :(得分:0)
原因是您从不将任何数据附加到StringBuilder。
我想你的意思是这样的:
buffer.Append(textBox1.Text);