Question about String.Equals()
Why the following code(#1) returns false but code (#2) returns true.
(#1)
string temp = sbTemp.ToString();
return temp.Equals(expectedOutputStream.ReadToEnd());
(#2)
string temp = sbTemp.ToString();
string expectedOutput = expectedOutputStream.ReadToEnd();
return temp.Equals(expectedOutput);
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

jingeRank #2,527 · 554 reputationPosted Oct 21, 2009, 6:13 PM EDT
The best way to know why is to place a breakpoint in "temp.Equals(expectStream.ReadToEnd())", and see what expectStream.ReadToEnd() returns.
Actually, It's not a good practice to write code like that. expectStream.ReadToEnd() may throw exceptions. So the statement is not stable.
I think the problem is caused by not closing the stream using expectStream.Close method.
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Oct 21, 2009, 8:41 PM EDT
I could not recreate the said problem. Anyway it seems when you directly call 'ReadToEnd()' it returns some unnecessary characters too with the expected outout. If you call a Trim() and see whether it works.
expectedOutputStream.ReadToEnd().Trim()