Part 23: Difference between String & String Builder
String :
String :
- A string is represented by class System.String.
- System.String is immutable.Immutable means if you create string then you cannot modify it and It always create new object of string type in memory.
- string object like add or modify an existing value, then it will simply discard the old instance in memory and create a new instance to hold the new value.
- A string (namespace: System.String ) is a sequential collection of Unicode characters that represents text.
- The maximum size of a String object in memory is 2 GB (about 1 billion characters).
- String is slower as compare to string builder object when we deals with large size of strings
- String is efficient when we deals with static or smaller size of string means we don’t need to modify later on.
String Builder :
- String Builder is sequence of characters or array of characters.
- String Builder is mutable.if you create string then you can modify it and It does not create new object of string type in memory.
- If any changes made to the string Builder is dynamic object like append an existing value, then it will not create another memory location simply It doesn't create a new object in the memory but dynamically expands memory.
- String Builder is located in the System.Text namespace.
- If any changes made to the string Builder is dynamic object like append an existing value, then it will not create another memory location simply It doesn't create a new object in the memory but dynamically expands memory.
- The default capacity of this object is 16 characters, and its maximum capacity is more than 2 billion characters.
- String Builder is faster as compare to string object when we deals with large size of strings
- String Builder is efficient when we deals with dynamic or larger size of string.