ReferenceEquals working wrong with strings

前端 未结 4 2056
眼角桃花
眼角桃花 2020-12-05 21:03

Why in this situation ReferenceEquals method of object behaves differently?

string a= \"fg\";
string b= \"fg\";
Console.WriteLine(object.Referen         


        
相关标签:
4条回答
  • 2020-12-05 21:33

    It is behaving correctly in both cases.

    The reason a and b are the same string object is because the compiler has noticed that you specified the same string twice, and has reused the same string object to initialize both a and b.

    This will generally happen with every string constant in your application.

    0 讨论(0)
  • 2020-12-05 21:35

    The first example has a compile time constant "fg" that is referenced by two variables. Since this is a compile time constant, the two variables reference the one object. The references are equal.

    Read into the topic of string interning for more on this behavior. As a starter, consider:

    For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.

    http://msdn.microsoft.com/en-us/library/system.string.intern.aspx

    In the second example, only one is a compile time constant, the other is a result of some operations. a and d do not reference the same object, so you get the false result from ReferenceEquals.

    0 讨论(0)
  • 2020-12-05 21:35

    Since you reference the same literal ("fg"), both your strings will actually point at the same thing. Please take a look at this article: http://csharpindepth.com/Articles/General/Strings.aspx (paragraph "Interning").

    Regards, Piotr

    0 讨论(0)
  • According to this post it has to do with something called interning. a and b are in your case two variables pointing to the same instance, that's why ReferenceEquals is returning true.

    0 讨论(0)
提交回复
热议问题