String Vs String Builder:
First thing you have to know that In which assembly these two classes lives?
So,
string is present in System
namespace.
and
StringBuilder is present in System.Text
namespace.
For string declaration:
You have to include the System
namespace.
something like this.
Using System;
and
For StringBuilder declaration:
You have to include the System.text
namespace.
something like this.
Using System.text;
Now Come the the actual Question.
What is the differene between string & StringBuilder?
The main difference between these two is that:
string is immutable.
and
StringBuilder is mutable.
So Now lets discuss the difference between immutable and mutable
Mutable: : means Changable.
Immutable: : means Not Changable.
For example:
using System;
namespace StringVsStrigBuilder
{
class Program
{
static void Main(string[] args)
{
// String Example
string name = "Rehan";
name = name + "Shah";
name = name + "RS";
name = name + "---";
name = name + "I love to write programs.";
// Now when I run this program this output will be look like this.
// output : "Rehan Shah RS --- I love to write programs."
}
}
}
So in this case we are going to changing same object 5-times.
So the Obvious question is that ! What is actually happen under the hood, when we change the same string 5-times.
This is What Happen when we change the same string 5-times.
let look at the figure.
Explaination:
When we first initialize this variable "name" to "Rehan" i-e string name = "Rehan"
this variable get created on stack "name" and pointing to that "Rehan" value.
after this line is executed: "name = name + "Shah". the reference variable is no longer pointing to that object "Rehan" it now pointing to "Shah" and so on.
So string
is immutable meaning that once we create the object in the memory we can't change them.
So when we concatinating the name
variable the previous object remains there in the memory and another new string object is get created...
So from the above figure we have five-objects the four-objects are thrown away they are not used at all. They stil remain in memory and they occuy the amount of memory.
"Garbage Collector" is responsible for that so clean that resources from the memory.
So in case of string anytime when we manipulate the string over and over again we have some many objects Created ans stay there at in the memory.
So this is the story of string Variable.
Now Let's look at toward StringBuilder Object.
For Example:
using System;
using System.Text;
namespace StringVsStrigBuilder
{
class Program
{
static void Main(string[] args)
{
// StringBuilder Example
StringBuilder name = new StringBuilder();
name.Append("Rehan");
name.Append("Shah");
name.Append("RS");
name.Append("---");
name.Append("I love to write programs.");
// Now when I run this program this output will be look like this.
// output : "Rehan Shah Rs --- I love to write programs."
}
}
}
So in this case we are going to changing same object 5-times.
So the Obvious question is that ! What is actually happen under the hood, when we change the same StringBuilder 5-times.
This is What Happen when we change the same StringBuilder 5-times.
let look at the figure.
Explaination:
In case of StringBuilder object. you wouldn't get the new object. The same object will be change in memory so even if you change the object et say 10,000 times we will still have only one stringBuilder object.
You don't have alot of garbage objects or non_referenced stringBuilder objects because why it can be change. It is mutable meaning it change over a time?
Differences:
- String is present in System namespace where as Stringbuilder present
in System.Text namespace.
- string is immutable where as StringBuilder is mutabe.