DIFFERENCE BETWEEN STRING AND STRING BUILDER IN C#:
- Get link
- X
- Other Apps
Posted by Ganesan - +919042710472
STRING: String is immutable, which means once you create an instance of string you cannot change the string's value. Then this will keep the old instance and create a new instance with a new value in the memory.
Example:
String strMyValue ="Hello Visitor"; //create a new string instance instead of changing the old one.
strMyValue +="How Are";
strMyValue +="you?";
STRINGBUILDER: String builder is mutable, it means if you change the value of string using string builder, it will not create a new instance, instead .it will update the value in the existing instance.
Example:
StringBuilder SbMyValue =new stringBuilder(" ");
Sb MyValue.Append("Hello Visitor");sss
Sb MyValue.Append("How Are You?");
String strMyValue= sbMyvalue. Testing();
NAMESPACE
STRING: The string class in the System namespace.
STRING BUILDER: The string builder class is in the system.Text namespace.
PERFORMANCE
STRING: Slow as compared to string Builder
STRING BUILDER: Fast, as compared to string like it, is faster than string while concatenating many strings together in a loop.
THREAD SAFETY
STRING: Thread-safe
STRING BUILDER: Not thread-safe
SYNCHRONIZATION
STRING: Provides synchronization
StringBuilder: It does not provide synchronization
MEMORY USAGE
STRING: Requires more, memory to create a new instance if operations performed on string change its value.
STRING BUILDER: It requires less memory as it updates the existing instance.
NATURE OF OBJECT
STRING: Object of the string is read-only nature
STRING BUILDER: The object of string builder is dynamic in nature.
EFFICIENCY
STRING: String class is less efficient as compared to StringBuilder while working with a large number of string concatenations.
STRING BUILDER: String Builder is more efficient as compared to string while working with a large number of string concatenations.
Article by Maria Academy
- Get link
- X
- Other Apps
Comments
Post a Comment