A program of StringBuilder and append and a basic concept

Source: Internet
Author: User

Just two are looking, found that there is an author upload a program (estimated that the original program author also did not run directly into the, I directly paste the run when several errors, I have modified a bit), the program

classPreson { Public stringFirstName;  Public  stringMiddleName;  Public  stringLastName;//define 3 Private variables FirstName middlename lastName            /*Public Preson (string fn,string mn,string ln)//constructor {FIRSTNAME=FN;            MIDDLENAME=MN;            LASTNAME=LN; }*/             Public voidDisplayfullname ()//Define a method name{StringBuilder FullName=NewStringBuilder ();//Instantiate FullNamefullname.append (firstName); //FullName =fullname+firstname Full is empty all only display firstNameFullname.append (" ");//FullName =fullname + "space" FullName already contains the value of FirstName            if(middlename[0] !=NULL)//if the first array of middle names printable empty then run down or jump out of this if statement{fullname.append (".");//If the condition is met, continue FullName = FullName + "."fullname.append (lastName); //fullname= FullName + LastName ValueConsole.WriteLine (FullName); //Output FullNameConsole.readkey (); }            }    }

A constructor is a supplement to a class, remember that it is a class, and you can refer to the difference between my other classes and structs. Of course, some words convenient, specific to see the situation

On the main program

 Static voidMain (string[] args) {            //Preson me = new Preson ("Bradley", "Lee", "Jones"); //instantiation ofPreson me =NewPreson (); Me.firstname="Liu"; Me.middlename="Lin"; Me.lastname="ru"; Me.displayfullname (); //Call this function                    }

Program may be a bit of a problem display not full name, I also bother to tune, interested in their own debugging, this program in fact the biggest meaning is the application of the constructor, hehe

——————————————————————————————————————————————————————————————————————————————————————————————————————————————— ———————————————

The following is the explanation of StringBuilder from Baidu explained very clearly behind will give the link

The StringBuilder class is a class that is used directly for string manipulation, for example
(1) string aa= "123456";
(2) aa+= "789";

(3) StringBuilder text=new StringBuilder ("123456", 12);
(4) text. Append ("789");
If you output AA, and text you will find that their output content is the same.
But the operation of AA is actually: first allocate an address space in memory, the space size is 6.
Then execute aa+= "789", the procedure is the connection string, "123456" and "789" and the address is reassigned in memory. Point the memory address of the AA to the memory address of "123456789".

That is, in memory, there is actually two space North allocation, the first memory space, in the future by the C # garbage disposal mechanism to automatically dispose of,

If we use 3 4 sentences to implement this process, then he is not allocating memory space again,
He is in the text of the memory space in the operation. Here to illustrate the next StringBuilder in the life variable process is can we ourselves to allocate his size, if the actual content beyond the memory space,
He'll double it automatically.

From the above example, we can know that the superiority of StringBuilder is in:
First: He doesn't need to allocate memory space every time. Therefore, the system does not need to deal with garbage;
Second: When we need to do multiple operations on a string multiple times, he is much more efficient than string

Http://zhidao.baidu.com/link?url=lMw8QIvJx-7_G-swkgYC4-HgFyDFY9jGjpw2Ft1IAOptJV_jQt6zpsW_RaVTpU4z0p_ Zf8-p1n8p7ayeuhlnba (the author explains it so clearly that I doubt that Microsoft is saying it, and then I feel so much more worried)

Append the words to understand it

A program of StringBuilder and append and a basic concept

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.