AspnetO

We code, that works!

  • Home
  • Asp.net
  • MVC
  • Interview Questions
You are here: Home / Asp.net / What is the difference between String and StringBuilder in Asp.net C# Vb.net?

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

By: Mayank Modi | Falls In: Asp.net, C#, Interview Questions, VB | Last Updated: Oct 10, 2020

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.

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.

 

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.

Leave a Reply Cancel reply

Search Your Topic



Social Connections

  • 1,438 Fans
  • 3,098 Followers
  • 51 Followers
  • 1,559 Subscribers

Get Latest Tutorials For Free



Top Posts

  • CSS3 Transition: fadeIn and fadeOut like Effects to Hide Show Elements
  • Asp.net TextBox: How to Get Set TextBox Value or Text in JavaScript
  • Show Confirm Message Box from Code-behind in Asp.net
  • Show Alert Message Box from Code-behind in Asp.net C# Vb
  • Asp.net TextBox: How to Get Set TextBox Value or Text in jQuery

Contribute to AspnetO

If you want to contribute your unique blog articles or tutorials (Free / Paid) to AspnetO in any languages, you're most welcome. Just send me your previous articles, and topics on which you are interested to post an tutorial. Contact us at email listed in contact us page. Selected candidates will be contacted.

Search 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

<

Recent Posts

  • Main Difference between SessionState and ViewState in Asp.net
  • How to Get appSettings Value from Web.config File?
  • How to Get ConnectionString from Web.config in Asp.net?
  • Difference between appSettings and connectionStrings in Web.config
  • Get Folder Files List and Export to CSV in .NET
  • Get Files List From Directory Recursively in C# Vb.net
  • Get Hash Value From Current Page URL In jQuery
  • Handle Multiple Submit Buttons in Single MVC Form

Copyright © 2014 - 2021 · All Rights Reserved.

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