String Example
string stringObj = "Hello Sam";
// create a new string instance instead of changing the old one
stringObj += " How Are";
stringObj += " You ??";
// create a new string instance instead of changing the old one
stringObj += " How Are";
stringObj += " You ??";
Console.WriteLine(stringObj );
Stringbuilder
Represents a mutable string of characters. This class cannot be inherited.
StringBuilder is mutable, means when you create string builder object you can perform any operations like insert, replace or append without creating new instance for every time. It will update string at one place in memory doesn't create new space in memory.
Consider using the StringBuilder class under these conditions:
When you expect your code to make an unknown number of changes to a string at design time (for example, when you are using a loop to concatenate a random number of strings that contain user input).
When you expect your code to make a significant number of changes to a string.
StringBuilder Example
StringBuilder stringBuilderObj = new StringBuilder("");
stringBuilderObj .Append("Hello Sam");
stringBuilderObj .Append("How Are You ??");
StringBuilder stringBuilderObj = new StringBuilder("");
stringBuilderObj .Append("Hello Sam");
stringBuilderObj .Append("How Are You ??");
Console.WriteLine(stringBuilderObj.ToString());
No comments:
Post a Comment