C # string

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.