如何在C#中的运行时为函数参数赋值默认值

Saa*_*ram 0 c# arguments function

我有以下课程:

public class Location
{
    private int X { get; set; }
    private int Y { get; set; }
    private int Z { get; set; }

    public UpdateLocation(int X, int Y, int Z)
    {
        this.X = X;
        this.Y = Y;
        this.Z = Z;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,有时我需要更新其中一些参数和所有参数.所以我在考虑一种初始化函数参数的方法,而不是将它们分配给局部变量,如:

public UpdateLocation(int X = this.X, int Y = this.Y, int Z = this.Z)
{
    this.X = X;
    this.Y = Y;
    this.Z = Z;
}
Run Code Online (Sandbox Code Playgroud)

所以我可以像这样调用函数:

UpdateLocation(Z:1509);
Run Code Online (Sandbox Code Playgroud)

但显然,这不起作用,因为参数默认值必须是编译时常量.任何想法如何解决这个问题,而不创建三个不同的更新功能(或更多)来更新这些变量?

Rab*_*uin 5

public UpdateLocation(int? X = null, int? Y = null, int? Z = null)
{
    this.X = X ?? this.X;
    this.Y = Y ?? this.Y;
    this.Z = Z ?? this.Z;
}
Run Code Online (Sandbox Code Playgroud)

您也可以设置属性.