Thursday, January 8, 2015

Best Practices for Using Strings in the .NET Framework

http://msdn.microsoft.com/en-us/library/dd465121.aspx

Use string.Equals()
-----------------------------------
string s1 = "test";
string s2 = "test";
string s3 = "test1".Substring(0, 4);
object s4 = s3;
Debug.WriteLine("{0} {1} {2}", object.ReferenceEquals(s1, s2), s1 == s2, s1.Equals(s2));
Debug.WriteLine("{0} {1} {2}", object.ReferenceEquals(s1, s3), s1 == s3, s1.Equals(s3));
Debug.WriteLine("{0} {1} {2}", object.ReferenceEquals(s1, s4), s1 == s4, s1.Equals(s4));

output:
True True True
False True True
False False True

-----------------------------------








No comments:

Post a Comment