C# getter setter 默认值

C# getter setter default value

如何设置 setter getter 的默认值?我想在设置 setter.

时做一些操作
public bool spin {
    get { return this.spin; }
    set {
        if (value == false) this.spinBack = true;
        this.spin = value;
    }
}
private bool spinBack;

我在 Unity3D 上尝试过这个,但在尝试这样做时遇到了这个错误。

WhosebugException: The requested operation caused a stack overflow.

我尝试只设置 getter 并将 getter 保留为默认值

public bool spin {
    get;
    set {
        if (value == false) this.spinBack = true;
        this.spin = value;
    }
}
private bool spinBack;

但我收到此错误

'spin.get' must have a body because it is not marked abstract, extern, or partial

WhosebugException 是由于您的 this.spin = value; 行递归设置 spin

改为使用支持字段:

public bool Spin 
{
    get { return _spin; }
    set {
        if (value == false) this.spinBack = true;
        _spin = value;
    }
}

private bool _spin;
private bool spinBack;