Simple trick on string data type to improve the performance of the application

Vishal Pathak
Abhima C# Programming
3 min readJan 8, 2022

As a developer performance is always a big concern. There are many tricks available but this one is very simple. So before we start let’s understand string Data type in C#.

Photo by xandtor on Unsplash

STRING:-

String Data type is used to store the sequence of characters. This is Reference type data type means it holds the memory address of the actual data. While defining the value to the string variable, the value should be enclosed in double quote (“”).

There are two types of string in C# i.e. Immutable string and Mutable string. Now let’s understand both the types.

Immutable String:-

Immutable string is type in C# which says if one object is having some string value in it and if we change the value, it will not the change the actual value as it is read only.

Mutable String:-

Mutable string can be changed if we change the value. So to have a mutable string we use StringBuilder in C#.

No let’s understand how internally String and StringBuilder are different from each other.

When we assign any value to String object GC will assign address of the value to the object and then if we change the value of String object it will assign address of the new value to the object. This means that the value is still exist which will be deleted by the GC after doing analysis on the value which makes the GC busy and RAM memory will also be occupied by the value which is useless now. To understand this how memory is getting wasted see the below image.

Immutable string

As it is shown in the above image that 3 kb of memory is getting wasted. This is just an example it can go in GB in real application deployed on production. So now let’s understand how String Builder resolves this issue.

StringBuilder:-

StringBuilder object stores the string value and if change the value then it will dynamically change the size of the memory and replace old value with the new value. Because of StringBuilder now the old value instantly removed from the memory. If we put in String builder in above diagram then the memory will hold only 2 kb of the data not more than that.

We should use StringBuilder where we know that the value of the string variable is gonna change many times.

StringBuilder is a life saver of many developer and very easy to implement. Below is the example where StringBuilder is getting used instead of String.

Conclusion:-

As you can see in above code we have option to clear the previous value and then insert the new value. We can even append the new value.

Thank you for reading this article. Also, if you found my content useful please like, comment and share the article with you friends.

Bhagavad Gita Verse of the Day

महाभूतान्यङ्ककारो बुद्धिरव्यक्त मेव च |
इन्द्रियाणि दशैकं च पञ्च चेन्द्रियगोचरा: || 6||

mahā-bhūtāny ahankāro buddhir avyaktam eva cha
indriyāṇi daśhaikaṁ cha pañcha chendriya-gocharāḥ

BG 13.6: The field of activities is composed of the five great elements, the ego, the intellect, the unmanifest primordial matter, the eleven senses (five knowledge senses, five working senses, and mind), and the five objects of the senses.

--

--

Vishal Pathak
Abhima C# Programming

love ❤ coding, solving some industry problems technologies: JavaScript, C#, Angular, PLSQL, Docker Want to learn: Python, Go language, AI, ML and Cloud