Many people ask me that how they can improve their website's performance? There are many ways through which you can improve the performance of your website but today, I will discuss the performance improvement with Strings.
In my opinion String is the most frequently used data type compared to other data types available. But there is a big problem with string, it is immutable. An immutable object is an object which cannot be modified. So whenever we try to change anything with string a new string object is created. These frequent creations of objects degrade the system's policy.
Also there are situations when we can control the case of a string i.e. generally we add 'M' or 'F' for gender. Now we can ensure that throughout application we either use capital 'M' or small 'm' to define male. A good approach is to use const in such cases.
In my opinion String is the most frequently used data type compared to other data types available. But there is a big problem with string, it is immutable. An immutable object is an object which cannot be modified. So whenever we try to change anything with string a new string object is created. These frequent creations of objects degrade the system's policy.
Avoid using "" as empty string
Every time when you write "" a new string object is created. But if you use string.Empty it will not create any additional string before assignment.//bad practice
string str = "";
//good practice
string str1 = string.Empty;
Avoid .ToLower()/.ToUpper() for Comparison
Usually developers heavily use .ToLower() or .ToUpper() to perform case insensitive comparison. Instead you should use string.Compare function.//Bad Practice
string errorCode= "ec-1001";
if (errorCode.ToLower() == "ec-1001")
{
}
//good Practice
string errorCode1 = "ec-1001";
if (string.Compare( errorCode1,"ec-1001",true)==0)
{
}
Also there are situations when we can control the case of a string i.e. generally we add 'M' or 'F' for gender. Now we can ensure that throughout application we either use capital 'M' or small 'm' to define male. A good approach is to use const in such cases.
const string Male = "M";
const string Female = "F";
string myGender = Male;
Avoid multiple string concatenation
Whenever you have to modify a string many times then consider using StringBuilder instead of simple string. Every time when you modify a string, a new object would be created. However, StringBuilder is mutable object and it performs string modifications without creating multiple objects.//Bad practice
string text = "start";
text += "start 1";
text += "start 2";
text += "start 3";
text += "start 4";
text += "start 5";
for (int i = 0; i < 10; i++)
{
text += "text " + i;
}
//Good Practice
StringBuilder sb = new StringBuilder();
sb.Append("start");
sb.Append("start 2");
sb.Append("start 3");
sb.Append("start 4");
sb.Append("start 5");
for (int j = 0; j < 10; j++)
{
sb.Append( "text " + j);
}
Jon · 710 weeks ago
Zeeshan Umar · 704 weeks ago
Here are some clarifications:-
"" vs string.empty:-
whenever we use "" a new instance of string is created, however if we use string.empty same instance will be used that is why it is considered a good practice.
Avoid .ToLower()/.ToUpper() for Comparison:-
Whenever we call .ToLower()/.ToUpper() a new instance of string is created which occupy more memory and generate more work for Garbage Collector. That is why it is recommended to use string.Compare( errorCode1,"ec-1001",true). As this method contains a parameter which is designed for this specific purpose. Also we can avoid by ensuring that either we save data in either small case or upper case whenever possible.
Also concatenation of string objects always result in a new string object, and if do this quite frequently, lots of memory will be utilized and more work for garbage collector. So I recommend that if we have roughly 5+ string concatenation then go for StringBuilder.
Hopefully above will resolve your concern.