3 ms·
Depends. Based on my hazy memory of C#, if id is a struct/value type it is copied, but if it is a class type, there's nothing preventing the method call from m
by tdestan 10y ago
Depends.
Based on my hazy memory of C#, if id is a struct/value type it is copied, but if it is a class type, there's nothing preventing the method call from mutating it.
Which is unfortunate, I agree.
- louthy 10y ago> here's nothing preventing the method call from mutating it. There is, the class could be immutable. public class Vector { public readonly int X; public readonly int Y; public Vector(int x, int y) { X = x; Y = y; } public Vector With(int? X = null, int? Y = null) => new Vector(X ?? this.X, Y ?? this.Y); } I believe record-types are proposed for the next release of C#.
- benaadams 10y agoMutable class with immutable struct wrapper would be better on allocations; then pass in the struct wrapper.
- ttd 10y agoThis is correct. One of the things I miss coming from C++ is the ability to pass const references. Then you have the advantage of passing a reference instead of copying an object, and the compiler enforces immutability in the function using the reference. On a similar note, I also really miss the ability to define const class methods (i.e. instance methods which do not modify the instance on which they are invoked). Although, perhaps get-only accessors fill part of that niche in C#.