What is a string?
The string keyword in C# defines a string, which is helpful for holding a sequential collection of characters. Because the string is immutable, once it is formed, it cannot be altered.
A string object will simply generate a new instance to retain the value and trash the previous instance in memory if you do any operations on it, such as insert, update, delete, etc.
C# String example
string name;
name = "Jaimin Shethiya";
Console.WriteLine("{0} - {1}", nameof(name), name);
string fname = "Jaimin";
Console.WriteLine("{0} - {1}", nameof(fname), fname);
string empty = string.Empty;
Console.WriteLine("{0} - {1}", nameof(empty), empty);
String lname = "Shethiya";
Console.WriteLine("{0} - {1}", nameof(lname), lname);
string nullValue = null;
Console.WriteLine("{0} - {1}", nameof(nullValue), nullValue ?? "NULL");
The string and String keywords were used to define the string variables in this string example. Both keywords (string and String) have the same functionality. In this case, the String keyword is known by an alias, "string."
See our page on C# String with Examples to gain more knowledge about strings.
What is a string builder?
The string keyword in C# defines a string, which is helpful for holding a sequential collection of characters. Because the string is immutable, once it is formed, it cannot be altered.
A string object will simply generate a new instance to retain the value and trash the previous instance in memory if you do any operations on it, such as insert, update, delete, etc.
C# StringBuilder example
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Jaimin Shethiya");
stringBuilder.AppendLine("C# Corner Rank: 105");
stringBuilder.AppendLine("\nTechnology stacks: ");
stringBuilder.Append(".NET Core");
stringBuilder.Append(", C#");
stringBuilder.Append(", Microservices");
stringBuilder.Append(", SQL");
Console.WriteLine(stringBuilder.ToString());
See our page on StringBuilder in C# with Examples to gain more knowledge about string builder.
String vs StringBuilder in C#
As was mentioned, the collection of characters can be held or stored using the string and StringBuilder. The differences between string and StringBuilder in C# are listed in the following table.
String |
StringBuilder |
The sequential collection of characters can be stored in a string. |
To store a collection of characters, a StringBuilder is equivalent to a string. |
The string is available with System. String namespace. |
The StringBuilder is available with System. Text namespace. |
Once a string is produced, it cannot be altered; it is immutable. |
After it is created, the StringBuilder can be changed because it is mutable. |
StringBuilder and string performance are nearly the same. It is always best to utilize string for a certain number of operations. |
When you do a lot of modify operations on string, StringBuilder's performance marginally outperforms string; otherwise, it's negligible. |
If you want to learn more about the string and string builder, then please refer to the below articles.
We learned the new technique and evolved together.
Happy coding!