Hacker News new | ask | show | jobs
by louthy 3385 days 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#.
1 comments

Mutable class with immutable struct wrapper would be better on allocations; then pass in the struct wrapper.