Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

In the opinon of programers why is string builder used instead of a string?

user-image
Question ajoutée par Ubaid sayeed Mohd abdul sayeed , Senior Programmer , Moksha solutions
Date de publication: 2015/12/20
Nisha Salim
par Nisha Salim , Software Engineer , Virtus IT Service

When you add something to a string, a new address in the memory is being allocated. The previous string is copied to a new location with the newly added part.In case of StringBuilder which keeps the same position in the memory without performing the copy operation. That means string is inefficient.

Example:

string str = "hi";

// create a new string instance instead of changing the old one

str += "hai1";

 

str += "hai2";

 

//StringBuilder working

StringBuilder sb = new StringBuilder("");

sb.Append("ha1");

sb.Append("hai2 ");

 

string str = sb.ToString();

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?