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);
Nilanka DharmadasaPosted Oct 22, 2009, 12:41 AM
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()
jingePosted Oct 21, 2009, 10:13 PM
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.