0

In this description article it is explained that

records are immutable by default

But records can be used just like a normal class:

using System;

record A
{
    public int u;
    public int U
    {
        set { u = value; }
    }
    internal void f() => u++;
}
record Program
{
    static void Main()
    {
        A x = new A() { u = 1000 };
        x.U = 100;
        x.f();
        Console.WriteLine(x);
        Console.ReadKey();
    }
}

What is meant by the immutability of a record?

Minimus Heximus
  • 2,683
  • 3
  • 25
  • 50
  • 1
    I think _by default_ is the crucial detail here - nothing stops you from explicitly making a `record` type mutable (by declaring a setter for example) – Mathias R. Jessen Jan 14 '21 at 17:01
  • @MathiasR.Jessen What's the difference between an immutable class and an immutable record about immutability? – Minimus Heximus Jan 14 '21 at 17:07
  • 1
    _"What is meant by the immutability of a record?"_ -- exactly what it says. In your example above, _you_ added mutability. So you got a class that's not immutable. The `record` data type in C# doesn't prevent you from doing that. But the implicitly-generated members the C# compiler provides all preserve immutability. See duplicate for more details on what's actually meant by "immutable" and how to go about ensuring that (or preserving it, in the case of `record`) – Peter Duniho Jan 14 '21 at 19:33
  • @PeterDuniho Clearly I did not ask about immutability. I asked about the immutability of a record. And that duplicate mark is completely misleading to an unrelated question. Why Microsoft says "records are immutable by default"? Why they don't say that about classes? This is what I asked. – Minimus Heximus Jan 14 '21 at 21:03
  • 1
    _"I did not ask about immutability"_ -- that's clearly false. _"Why they don't say that about classes?"_ -- because the compiler isn't generating code for you in regular classes. Classes aren't _anything_ "by default", so there's no need to clarify. Beyond that, you would need to ask the author of that passage if you want to really know _why_ they worded it exactly that way, which is a question that is decidedly not suitable for Stack Overflow. Considering your question as a duplicate is being generous, IMHO. – Peter Duniho Jan 14 '21 at 21:33
  • @PeterDuniho Don't be generous to me. If this question about what is meant by "immutable by default of record" is not suitable for stackoverflow close it with a correct mark. – Minimus Heximus Jan 14 '21 at 21:51
  • In many cases, there is not just one single "correct mark" that may be used to close a question. Just because your question _could_ have been closed using some other reason does not mean that it _also_ should not be closed using the above reason. Since you seem to agree that the question should've been closed, it doesn't make any sense to debate the specific _reason_ used to close it. – Peter Duniho Jan 14 '21 at 22:06
  • @PeterDuniho Clearly I do not agree with closing a question about the standard docs on C#. I just said do not link a misleading question for any kind of generosity to me. – Minimus Heximus Jan 14 '21 at 22:17
  • @MinimusHeximus it looks like your question has more to do with understanding what `by default` means than anything related to programming. By default meant how a thing behaves unless you change it. It also means that you *can* change it. `Why they don't say that about classes` because that's not how classes work at all. `What is meant by the immutability of a record` the same it means for classes - you can't change the values once they're set. Except with classes you have to write a lot of code to make a class immutable. The details are already explained in the docs and blog posts on records – Panagiotis Kanavos Jan 15 '21 at 07:00
  • @MinimusHeximus personally, I'd close this question as `unclear`. There's no ambiguity in the docs about what immutability means, so what is the actual question? Did you have some other meaning for `immutability` in mind? – Panagiotis Kanavos Jan 15 '21 at 07:05
  • I found out that: "immutable by default" just means that records have a few methods and properties automatically added which make it easier for the programmer to use them as immutable classes. Otherwise, they are just classes, and immutability by default has no other meanings or effects. – Minimus Heximus Jan 15 '21 at 10:36

0 Answers0