C# StringBuilder

String string and StringBuilder in C-Sharp

Hello friends, Today we are going to learn, What is the difference between String and string? and What is the difference between string and StringBuilder?

You can also learn about SOLID Principle(s) in details.

One of the questions that makes many C# programmers novice is:

What is the difference between string and String?


In C#, string is an alias for the String class in .NET framework. Like a person having two names one for UID, social, public, documentation and official purpose and other for parents like to call him with love at home.

Every C# type has an equivalent in .NET.

For example, short, int and long in C# map to Int16, Int32 and Int64 in .NET respectively.

Technically there is no difference between string and String both are same.

The only tiny difference is that if you want to use the String class, you need to import the System namespace on top of file, whereas no need for string keyword.

Many developers prefer to declare a string variable with string keyword. but make good practice with String class when you needs to access one of its static members.

String.Format();

Another common questions that makes many C# programmers novice is:

What is the difference between string and StringBuilder ?


String : String is immutable, which means if you create string object then you cannot modify it and It always create new object of string type in memory.

using System; namespace ConsoleApp1 { class Program1 { public static void Main() { string msg = "Welcome Visitor"; // create a new string instance instead of changing the old one msg += " here on" ; // again create a new string instance instead of changing the old one msg += " SW Class."; Console.Write(msg); Console.ReadKey(); } } } //Output: Welcome Visitor here on SW Class.

Stringbuilder:

StringBuilder is mutable, it means if you create StringBuilder object then you can have more functionality over string by performing any operation like insert, replace or append without creating new instance for every time like in string. it will update string at one place in memory doesn't create new space in memory.

For using StringBuilder you needs to import System.Text names on top of C# program file as below.

using System.Text;

We can create StringBuilder object as follows.

StringBuilder sbMyValue = new StringBuilder("");

using System; using System.Text; namespace ConsoleApp1 { class Program1 { public static void Main() { StringBuilder msg = new StringBuilder(); // Adding some string msg.Append("Welcome Visitor"); // same instance is used to append previous string msg.Append(" here on"); // again same instance is used to append previous string msg.Append(" SW Class."); Console.Write(msg.ToString()); Console.ReadKey(); } } }

Append(): Appends the string representation of a specified object to this instance. append method is used to add text at the end of string with same instance by preserving its previous value.

ToString(): It converts the value of this instance (Object) into string.

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories