三个运算符导致 StackOverflowException
Three operator causes StackOverflowException
我知道,在这种情况下我可以简单地使用 abs,但我只是好奇:为什么会这样?
public float maxThrotle{
set { maxThrotle = value < 0 ? -value : value; //this line causes problem
}
get { return maxThrotle; }
}
您试图从 属性 setter.
中调用 属性 setter 导致无限循环
您可能想要创建一个私有支持字段来存储值,如下所示:
private float maxThrotle;
public float MaxThrotle {
set { maxThrotle = value < 0 ? -value : value; //this line causes problem
}
get { return maxThrotle; }
}
请注意,根据大多数 C# 编码标准,我将 属性 重命名为使用大写字母。
(此外,单词 throttle 拼写为双 -t-)。
我知道,在这种情况下我可以简单地使用 abs,但我只是好奇:为什么会这样?
public float maxThrotle{
set { maxThrotle = value < 0 ? -value : value; //this line causes problem
}
get { return maxThrotle; }
}
您试图从 属性 setter.
中调用 属性 setter 导致无限循环您可能想要创建一个私有支持字段来存储值,如下所示:
private float maxThrotle;
public float MaxThrotle {
set { maxThrotle = value < 0 ? -value : value; //this line causes problem
}
get { return maxThrotle; }
}
请注意,根据大多数 C# 编码标准,我将 属性 重命名为使用大写字母。
(此外,单词 throttle 拼写为双 -t-)。