Why strings does not compare references?

后端 未结 5 1087
耶瑟儿~
耶瑟儿~ 2021-01-11 23:54

I know it is special case but why == between strings returns if their value equals and not when their reference equals. Does it have something to do with overlloading operat

5条回答
  •  天涯浪人
    2021-01-12 00:07

    on a string, == compares by value

    "Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references (7.9.7 String equality operators). This makes testing for string equality more intuitive."

    In short, == on strings compares the strings by value, not by reference, because the C# specification says it should.

提交回复
热议问题