AspnetO

We code, that works!

  • Home
  • Asp.net
  • MVC
  • Interview Questions

What is the difference between String and StringBuilder in Asp.net C# Vb.net?

By: Mayank Modi | Folls In: Asp.net, C#, Interview Questions, VB | Last Updated: Aug 29, 2014

In my previous tutorials, I’d explained the difference between string and String, difference between executereader executenonquery and executescalar, difference between dataset datareader dataadapter and dataview and other similar tutorials on Difference, Asp.net here.

Now here in this tutorial, I’ll explain the main difference between String and StringBuilder with examples for understanding in asp.net c# as well as vb.net.

Note: The difference between String and StringBuilder is not limited to c# or vb.net languages but I’ll cover only those two.

Main Differences between String and StringBuilder

String StringBuilder
String is immutable, meaning that once you created string object then you can’t modify it StringBuilder is mutable, meaning that once you created string builder object then you can modify or append without creating a new instance for every time
Any operation that appears to change the string, it creates a new instance of string type in memory Any operation that appears to modify or append new string, it doesn’t create a new instance of string type in memory
Required Class is System.String Required Class is System.Text.StringBuilder
Required Namespace is System Required Namespace is System.Text
String is slower as compare to string builder object when we deals with large size of strings StringBuilder is faster as compare to string 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 StringBuilder is efficient when we deals with dynamic or larger size of string means we want to modify that string later on

Example of String – [C#]

Here is the example of String in C#:

//efficient, we don't need to modify it later on
string strEfficientTest = "AspnetO – Way To Learn Asp.net";

//not efficient as it'll create new instance while appending sting
string strNotEfficientTest = "AspnetO";
strNotEfficientTest += " – Way To ";
strNotEfficientTest += "Learn Asp.net";

Example of String – [Vb.net]

Likewise, following is the example of String in vb.net:

'efficient, we don't need to modify it later on
Dim strEfficientTest As String = "AspnetO – Way To Learn Asp.net"

'not efficient as it'll create new instance while appending sting
Dim strNotEfficientTest As String = "AspnetO"
strNotEfficientTest &= " – Way To "
strNotEfficientTest &= "Learn Asp.net"

Here in the above example, The strEfficientTest is more efficient than strNotEfficientTest because memory being allocated for strNotEfficientTest when it initially assigned the value of “AspnetO” and when you append ” – Way To ” and “Learn Asp.net” to it, then the .NET framework will allocate a new memory location for it to be able to combine all in one as “AspnetO – Way To Learn Asp.net”.

Example of StringBuilder – [C#]

Here is the example of StringBuilder in C#:

//efficient as it'll not create new instance while appending sting
StringBuilder sbEfficientTest = new StringBuilder("AspnetO");
sbEfficientTest.Append(" – Way To ");
sbEfficientTest.Append("Learn Asp.net");

//now you can get combined string by using this single statement
string strStringBuilder = sbEfficientTest.ToString();

Example of StringBuilder – [Vb.net]

Likewise, following is the example of StringBuilder in vb.net:

'efficient as it'll not create new instance while appending sting
Dim sbEfficientTest As New StringBuilder("AspnetO")
sbEfficientTest.Append(" – Way To ")
sbEfficientTest.Append("Learn Asp.net")

'now you can get combined string by using this single statement
Dim strStringBuilder As String = sbEfficientTest.ToString()

However with StringBuilder, The .NET framework only allocates the memory once for the sbEfficientTest variable when it initialize first time with the word “AspnetO”, when you append the string ” – Way To ” and “Learn Asp.net” to it, it’ll still share the same memory location.

Ultimately, as per following example you’ll notice no difference at all in execution speed. However, take a look at the following example:

string strTest = string.Empty;
for (int i = 0; i < 10000; i++)
{
strTest += i.ToString() + " ";
}

You notice it’ll take longer than the following same example using StringBuilder:

StringBuilder sbTest = new StringBuilder();
for (int i = 0; i < 10000; i++)
{
sbTest.Append(i.ToString());
sbTest.Append(" ");
}

As you can see the first example will loop through “10000” and creating 10000 * 2 instances in memory allocation where as in case of StringBuilder it’ll give much less stress on the .NET framework memory allocator without creating new instances.

Signup Today And Get Latest Tutorials For Free!

Subscribe to us and get free latest tutorials notifications whenever we publish a new contents.

About Mayank Modi

Mayank is a web developer and designer who specializes in back-end as well as front-end development. He's a Founder & Chief Editor of AspnetO. If you'd like to connect with him, follow him on Twitter as @immayankmodi.

Share Your Comments & Feedback Cancel reply

Note: To post any code in comment, use <pre>your code</pre>

Social Connections

  • 0 Fans
  • 3,222 Followers
  • 21 Followers
  • 52 Followers
  • 1,559 Subscribers

Top Posts

  • CSS3 Transition: fadeIn and fadeOut like Effects to Hide Show Elements
  • Top 10 OOPS Concepts In C# .NET With Examples
  • Parser Error While Deploying Website to Server in Asp.net
  • Asp.net TextBox: How to Get Set TextBox Value or Text in jQuery
  • How to Print Asp.net GridView Data on Button Click using Javascript?

Find by Tags

Ado.net Ajax appSettings Asp.net C# CheckBox CheckBoxList ConnectionStrings Control CSS CSS3 Difference Download DropDownList Export Facebook fadeIn fadeOut fadeTo fadeToggle File File Extension FileUpload Function GridView IIS Interview Questions JavaScript jQuery MVC OOP RadioButtonList RDP Repeater Send Mail Solutions Split SQL Stored Procedure TextBox Upload Validation VB Web.config Web Hosting

The Man Behind AspnetO

Mayank Modi

Hi there,

Myself Mayank Modi, a Full Stack Developer (.NET Stack) and a blogger from Surat, India.

I'm welcoming you to my blog - AspnetO, a programmers community blog where we code, that works!

I started AspnetO as a hobby and now we're growing day by day. We're now having 5000+ programmers that get benefits and learn new things about website design and development under our community blog.

Here at AspnetO, I write about Beginners to Advance level of tutorials on programming languages like Asp.net using C# and Vb.net, MVC, SQL Server, JavaScript, jQuery etc. In sort, all about .NET Framework and website development stuff and sometimes sharing tips and tricks that can help you to grow up your programming skills.

You can get more details about me and my blog at About us page.

Subscribe To Newsletter

Enter your email address to subscribe to this blog and receive notifications of new posts right to your inbox

Join 1000+ other subscribers

Hot on AspnetO

Icon
Gridview Insert Update Delete Example in Asp.net, C#, Vb.net 7245 downloads 39.76 KB
Download This Example
Icon
Gridview Insert Update Delete Example in Asp.net, C#, Vb.net 6211 downloads 39.76 KB
Download This Example
Icon
Gridview Insert Update Delete Example in Asp.net, C#, Vb.net 6207 downloads 39.76 KB
Download This Example
Icon
Export gridview all rows or selected rows to word, excel, text, pdf examples 3056 downloads 1.01 MB
Download This Example
Icon
Export gridview all rows or selected rows to word, excel, text, pdf examples 2993 downloads 1.01 MB
Download This Example

Copyright © 2014 - 2019 · All Rights Reserved.replica cartier watches

About | Copyrights | Privacy | Terms | Contact | Advertise | Sitemap
Previous How to Print Asp.net GridView Data on Button Click using Javascript?
Next Auto-refresh Webpage in Every 10 Seconds in Asp.net C# Vb.net