What is the difference Between Assignment and Creating Instance of String in C#? -
What is the difference Between Assignment and Creating Instance of String in C#? -
i have sample code.
var charmass = new char[] { 's', 't', 'r' }; string mystring = new string(charmass); string mystring2 = new string(charmass); string mystring3 = "str"; string mystring4 = "str"; bool bb1 = object.referenceequals(mystring, mystring2); bool bb2 = object.referenceequals(mystring, mystring3); bool bb3 = object.referenceequals(mystring3, mystring4);
why bb1 , bb2 false? know equals must show true, because compares values, memory allocation strings? why mystring3 , mystring4 pointing same block of memory in heap mystring , mystring2 not?
c# compiler optimizes same literals point same string instance
msdn:
the intern pool conserves string storage. if assign literal string constant several variables, each variable set reference same constant in intern pool instead of referencing several different instances of string have identical values.
c# string heap
Comments
Post a Comment