How to copy/clone records in C# 9?
But what about cloning?
var r4 = r with { };
performs a shallow clone on r.
The clone method is public according to the specification above. But what is its name?
The C# compiler has a fairly common trick where it gives generated members names which are illegal in C#, but legal in IL, so that they can't be called except from the compiler, even if they're public. In this case the name of the Clone
method is <Clone>$
.
If so, what is the correct way to deep copy records?
Deep copying you're out of luck. However since records should ideally be immutable, there should be no difference in practice between a shallow copy, a deep copy, and the original instance.
It seems from the specification that one is able to create one's own clone method. Is this so, and what would be an example of how it should work?
Unfortunately this didn't make the cut for C# 9, but there's a strong chance it'll be in C# 10.