有两种方法可以逐行从RichTextBox读取数据
1)使用for循环遍历richtextBox的行
String s=String.Empty;
for(int i=0;i<richtextbox.lines.length;i++)
{
s=richTextBox.Lines[i]
}
Run Code Online (Sandbox Code Playgroud)
2)使用foreach循环枚举richTextBox.Lines集合
String s=String.Empty;
foreach(string str in txtText.Lines)
{
s=str;
}
Run Code Online (Sandbox Code Playgroud)
当我们使用foreach循环枚举richtextbox的数组集合时,性能存在巨大差异.
我尝试了15000行.for循环需要8分钟才能循环到15000行.而foreach只花了一秒钟来枚举它.
为什么会出现这种行为?
richtextbox ×1