Why is .Equal False when I expect it to be true?

The first line is true, the second is false. htmlOut and s2 are StringWriter objects.

    bool b = s2.ToString() == htmlOut.ToString();
    ret = htmlOut.Equals(s2); 

I was expecting true to be b, but why is ret false?

+3
source share
4 answers

StringWriter does not override object.Equals.

htmlOut.Equals(s2);

is equivalent to:

object.ReferenceEquals(htmlOut, s2);
+6
source

StringWriteruses internal StringBuilderfor recording. StringWriter.ToString()returns a string constructed with StringBuilder.

StringWriterdoes not cancel object.Equals(), therefore it StringWriter.Equals()compares if both objects are the same link, and not if their string representations are equal.

+9
source

Equals . htmlOut Equals ?

, , , , , , .

+2
htmlOut.ToString().Equals(s2.ToString());

it will return true

+1
source

Source: https://habr.com/ru/post/1713563/


All Articles