Does MS C# Implementation of String check ReferenceEquals of the immuteable base String first? -
suppose had string
s.
string = "this string"; strinb b = "this string"; string c = a;
as understand string a
, b
not nescessarily share same immuteable base string. string c
copy of a
, points internally same immuteable string.
if compare a
, b
equality, return true. @ least because represent same character sequence.
if compare a
, c
equality, return true. did check characters or did compare pointers immuteable string first?
edit:
to answer how check equality:
private void stackoverflowequals() { string = @"http://stackoverflow.com/questions/25932695/does-ms-c-sharp-implementation-of-string-check-referenceequals-of-the-immuteable"; string b = @"http://stackoverflow.com/questions/25932695/does-ms-c-sharp-implementation-of-string-check-referenceequals-of-the-immuteable"; string c = a; if (!(a == b)) throw new exception(); if (!(a == c)) throw new exception(); }
yes, does. here's source code equals:
[reliabilitycontract(consistency.willnotcorruptstate, cer.mayfail)] public override bool equals(object obj) { if (this == null) //this necessary guard against reverse-pinvokes , throw new nullreferenceexception(); //other callers not use callvirt instruction string str = obj string; if (str == null) return false; if (object.referenceequals(this, obj)) return true; if (this.length != str.length) return false; return equalshelper(this, str); }
for complete string class source code see this.
Comments
Post a Comment