4

I am a beginning C# programmer so bear with me. I have gone over the other entries re: immutable strings in C#, but they do not make sense to me. Regarding C# immutable strings, why did Microsoft not just do a constant modifier to string as in:

const string myString = "my-string";

Also, if mutable strings are so susceptible to so many hacks that they had to be made immutable, is that not the case also for the other types of variables as well?

jonsca
  • 10,218
  • 26
  • 54
  • 62
manuel
  • 41
  • 1
  • 3
    Very good question, actually. Look at this link: [Why are strings immutable in many programming languages?](http://stackoverflow.com/questions/9544182/why-are-strings-immutable-in-many-programming-languages) – paulsm4 Jan 05 '16 at 02:13
  • 1
    Constants can be bad, especially in libraries. When you compile with constants the literal string is compiled in. If a library updates the constant but you don't recompile against it you will still be getting the original string. – Crowcoder Jan 05 '16 at 02:24
  • As to your last sentence, the benefits of immutability on other types are exercised in some languages. In .Net world F# has immutable types for example. – PiotrWolkowski Jan 05 '16 at 04:43
  • @paulsm4 I'm speaking to why Microsoft did not dictate const modifier for strings, I don't know what your reply has to do with my comment. – Crowcoder Jan 05 '16 at 10:41

0 Answers0