Navigation

What is the difference between a mutable and immutable string in C#?

Mutable String, Immutable String, mutable and immutable string in c#, difference between a mutable and immutable string in C#, Immutable object, Mutable object

Introduction

In this article, I am explaining about OOPS component what is the difference between a mutable and immutable string in C#. I have explained with example about difference between a mutable and immutable string. Moreover, already explained about What is Boxing and Unboxing in C# in my earlier article. Here is showing another example of OOPS.

Immutable String

Immutable string can’t be alter, once we have given a value to immutable object state can’t be changed. We can use System.String to use immutable string.

   String str = “Technology Crowds”;  //*  Immutable String Example
   string str = “Technology Crowds” + “Technology”;

Mutable String

We can assign multiple values to changeable string and object state will be altered. Best example of changeable is stringbuilder object will be used. we will multiple concatenation to changeable string. changeable have superb flexibility to alter object state, changeable string will generate long string.
We can use given below namespace to use mutable string in c#

 using System.Text;   //*  namespace used to import string builder 

  StringBuilder strLong = null;   //*  [Mutable String] example
  strLong.Append("Technology");
  strLong.Append("Crowds");

Mutable vs Immutable
I have demonstrated with my best about what is the difference between a mutable and immutable string in C# that can help you better understand difference between a mutable and immutable string.

Conclusion

All string object are immutable in C#, String builder would allow us to work alternatively like as Mutable string in C#.

Best ASP.Net (C#) Comparisons:

Share

Anjan Kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

1 comments:

  1. Mutable: Mutable means whose state can be changed after it is created.
    Immutable: Immutable means whose state cannot be changed once it is created.

    String objects are 'immutable' it means we cannot modify the characters contained in string also operation on string produce a modified version rather than modifying characters of string.

    Use Reference
    imaginationhunt.blogspot

    IS STRING MUTABLE OR IMMUTABLE IN .NET?
    http://imaginationhunt.blogspot.in/2015/06/is-string-mutable-or-immutable-in-net.html

    ReplyDelete