There are two ways to read data from RichTextBox line by line
1 ) use a for loop to loop through lines of a richtextBox
String s=String.Empty;
for(in
I think the Lines
property is recalculated every time you want to access it. Consequently, the foreach
method performs the calculation only once, while every time your reference Lines[i]
it's re-evaluating the whole thing. Try caching the result of Lines
property and checking again:
String s = String.Empty;
var lines = richtextbox.Lines;
for(int i = 0; i < lines.Length; i++)
{
s = lines[i];
}
By the way, your question makes an implicit assumption that foreach
is always slower than for
. This is not always true.