-1

i am not understand the difference of this that string is immutable and string.builder is not mutable in c# . i just want to tell you i know the meaning of immutable and mutable in English but not understand in c# language because we can change string like this where is immutable concept than?

string a = "hello";
a="hello"+"world";
Console.WriteLine(a);

is there is any article that i read and understand with example big big thanks advanced of your reply.

  • 4
    `StringBuilder` **is** mutable. – Soner Gönül Mar 30 '15 at 14:39
  • 6
    "immutable" and "not mutable" mean the same thing :) – Lews Therin Mar 30 '15 at 14:41
  • Also [What is the difference between a mutable and immutable string in C#?](http://stackoverflow.com/questions/4274193/what-is-the-difference-between-a-mutable-and-immutable-string-in-c) – Soner Gönül Mar 30 '15 at 14:43
  • @DonkeyKong i think there is a better way to ask our on question i have different issue for this ok see my code i think you don't have any other work to do except pulling the vote down their are some students who are really wants to learn by their own ok no hard feelings – ramesh rukesh Mar 30 '15 at 14:47
  • @rameshrukesh, it's no different than any other variable assignment, immutable or not. You can always assign to a (non-readonly) variable. Integers are immutable, too, but you would never trip over `int i = 1; i = i + 1;` Strings -- and any other variable assignment -- are literally the same thing. String immutability is one of the most unnecessarily confused topics around, since it comes up more often than it needs to. – Anthony Pegram Mar 30 '15 at 14:47
  • What you need to know about string immutability, particularly versus a StringBuilder -- there are no mutating **methods.** Every mutation you do with a string produces a new string, doesn't modify the existing one. The fact that you might capture it and assign it to the same variable is the same concept as integers, longs, doubles, and Foos. – Anthony Pegram Mar 30 '15 at 14:49
  • @AnthonyPegram hi i am student of .NET i am just curios about what is a basic different string is also can change variable value and string.builder also change variable value – ramesh rukesh Mar 30 '15 at 14:53

1 Answers1

2

You're not "changing" the original string - you're creating a new string. By immutable it means that things like this:

a.ToUpper();

do not modify a - they return a new string, so with

b = a.ToUpper();

b and a are different strings.

In your example,

string a = "hello";
a="hello"+"world";
Console.WriteLine(a);

a is a variable that references a new string after the second line is executed.

D Stanley
  • 139,271
  • 11
  • 154
  • 219
  • but a is a same variable i mean it is first have "hello" than there is hello world and application will write hello hello world it is changed – ramesh rukesh Mar 30 '15 at 14:43
  • 2
    @Ramesh: Internally `a` will be pointing to a new address in memory. – Nikhil Agrawal Mar 30 '15 at 14:44
  • 3
    @rameshrukesh The *variable* is changed, yes. The string *value* isn't changing. – Servy Mar 30 '15 at 14:44
  • @rameshrukesh - no, the `string` `"hello"` didn't change. A *new* `string` `"hello world"` was created. There are now *two different* `strings`. – Corak Mar 30 '15 at 14:44
  • @rameshrukesh It is the same _variable_ but it's now referencing different _memory_. The original memory that stored `"hello"` in unchanged. Any operations on `string` must return a new string; they cannot change the string in-place. – D Stanley Mar 30 '15 at 14:46
  • 2
    @rameshrukesh Think of it like this: if you did `var c = a;` can any (valid) operation on `a` alter the effective value of `c`? If the type is `string`, then no, that's not possible, so `string` is immutable. If the type is `StringBuilder`, then yes, that is possible, so `StringBuilder` is mutable. –  Mar 30 '15 at 14:46
  • 2
    Think of a `string` in this case like a number. if you have `a = 1` and then say `a = a + 2` then you didn't change the number `1`, but you "created" a new number: `3`. -- (and *changed* the *contents* of the variable `a`. variables can change. `string` *variables* can change. But the `strings` themself can *not*) – Corak Mar 30 '15 at 14:47
  • @Corak hmm ok thanks to reply i am going to work on my application to better understand and yes i am also going to test your examle that you give – ramesh rukesh Mar 30 '15 at 14:50
  • @DStanley thanks for your answer i am going to try your example and understand it betterly – ramesh rukesh Mar 30 '15 at 14:51
  • @Corak where'd the space come from in your "hello world"? – Bill Woodger Mar 30 '15 at 14:52
  • If you're interested in the concept of immutability, I'd like to recommend eric lippert's [blog series](http://blogs.msdn.com/b/ericlippert/archive/2007/11/13/immutability-in-c-part-one-kinds-of-immutability.aspx) about it. -- @Bill ... creative freedom? ^_^; yeah, sorry of course there shouldn't be, given the example. – Corak Mar 30 '15 at 14:55
  • @Corak thanks for the link i am going to read this – ramesh rukesh Mar 30 '15 at 15:12