4

I have read the older topics like those ones:

But, I can't understand the meaning of the word immutable, if to compare it with the F# analogue:

open System
let data = "London"
data <- "123"
Console.WriteLine data

This one won't compile due the next reason:

error FS0027: This value is not mutable. Consider using the mutable keyword, e.g. 'let mutable data = expression'.

So... If to compare the F# behaviour with the C# one, seems to be F# has the real meaning of immutability or I don't understand the real meaning of this term...

Let's look at the C# variant:

class Program
{
    static void Main()
    {
        var a = "London";
        a = "123";
        System.Console.WriteLine(a);
    }
}

The output will be: 123.

Let's take information from the next article: Why are the strings immutable in .NET?

String appending

Honestly, if to look at this picture and compare it with the F# variant, I can't see the immutability... But also, I understand, that over 100+ people at SO (questions were published at the top of this question) can't be wrong and give the wrong name for such meaning.

Please, tell me why has been the C# string called immutable, if I can change its value? And if to compare it with the F# sample, the results are different for the single meaning.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • 3
    You can't change the value of an existing string, a new string is allocated with the new value. As the [string documentation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/string) says, _"Strings are immutable--the contents of a string object cannot be changed after the object is created, **although the syntax makes it appear as if you can do this.**"_ – stuartd Jan 12 '18 at 17:17
  • You can't change its value as seen in your last illustration were not the original string is modified but a new copy is created. – ckuri Jan 12 '18 at 17:18
  • 4
    `string` instances are immutable, F# let bindings are immutable but c# local variables are not. – Lee Jan 12 '18 at 17:19
  • 2
    @Neverlands just a quick note to the examples, `data = "123"` in 2nd case in F# is not the same as `a = "123"` in the C# variant - it's more like `a == "123"`, so it's a test, producing a bool, which then gets discarded. It has little to do with the immutability. – Honza Brestan Jan 12 '18 at 21:37
  • @HonzaBrestan I see, thanks for your comment, I shall remove that part. –  Jan 12 '18 at 23:09

1 Answers1

9

You are confusing the immutability of the variable in F# with the immutability of the object in C#.

In C#, you can change which object representing a string a variable is pointing at. But you can't change anything about that object, hence the object is immutable.

In F#, you cannot change which value a variable has, so the variable is also immutable.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • Would it be accurate to say that C#'s variable only references a string object, but the F# variable is the string object itself? (I've never worked in F#) – RToyo Jan 12 '18 at 17:29
  • 1
    I have only passing familiarity with F#, but according to MSDN F#'s string is the same old System.string so the F# variable would also hold a reference to an object. The difference in F# being that you can't change which object is being referenced, similar to a `const string` or readonly field in C#. https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/strings – Pete Kirkham Jan 12 '18 at 17:33
  • 2
    It should be worth noting that in the first F# case, the code will compile and work like the C# case if data is declared like so: `let mutable data = "London"`. So the mutability exception is due to the the fact that the *variable* data is immutable, which of course is in line with what you say in your answer. – Jonathon Chase Jan 12 '18 at 17:37