One of the most notable features of string is its constant invariance: once we have created a string and allocated a contiguous memory space on the managed heap, we will not be able to modify the string in any way to make it longer, shorter, or more formatted. Any string that is returned by all operations on the string, such as a string that calls ToUpper for uppercase, is actually another string that is recreated and does not itself produce any changes.
A string object is called immutable (read-only), because once the object is created, the value of the object cannot be modified. Sometimes it seems to change, actually is a string after a special processing, each time the value is changed to create a new string object, the variable will point to the new object, and the original is still pointing to the original object, so it will not change. This is also the reason why string is inefficient.
classProgram {stringstr ="Class.str"; Private inti =0; Static voidStringconvert (stringstr) {STR="string being converted"; } Static voidStringconvert (program c) {C.str="string being converted"; } Static voidADD (inti) {i++; } Static voidAddwithref (ref inti) {i++; } Static voidMain (string[] args) { intI1 =Ten; intI2 = -; stringstr ="Str"; Program C=NewProgram (); ADD (I1); Addwithref (refi2); ADD (C.I.); Stringconvert (str); Stringconvert (c); Console.WriteLine (I1); Console.WriteLine (I2); Console.WriteLine (C.I.); Console.WriteLine (str); Console.WriteLine (C.STR); Console.ReadLine (); }} Output:Ten + 0StrstringBeing converted
usingSystem.Runtime.InteropServices; usingSystem; namespaceConsoleprototype { Public classA { Public stringSTR1 ="AAA"; Public stringSTR2 ="AAA"; } Public classB { Public stringSTR1 ="AAA"; } classProgram {Static voidMain (string[] args) {A A=NewA (); GCHandle hander=GCHandle.Alloc (A.STR1); varPin =gchandle.tointptr (hander); GCHandle Hander2=GCHandle.Alloc (A.STR2); varPin2 =gchandle.tointptr (hander); b b=NewB (); GCHandle Hander3=GCHandle.Alloc (B.STR1); varPin3 =gchandle.tointptr (HANDER3); Console.WriteLine (PIN); Console.WriteLine (PIN2); Console.WriteLine (pin3); Console.ReadLine (); } } }
C # string