ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل Ubaid sayeed Mohd abdul sayeed , Senior Programmer , Moksha solutions
تاريخ النشر: 2015/12/20
Nisha Salim
من قبل 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();

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟