所以我有一个以下列方式定义的结构:
public struct Item
{
public string _name { get; set; }
public double _weight
{
get
{
return _weight;
}
set
{
_weight = value;
//Shipping cost is 100% dependent on weight. Recalculate it now.
_shippingCost = 3.25m * (decimal)_weight;
//Retail price is partially dependent on shipping cost and thus on weight as well. Make sure retail price stays up to date.
_retailPrice = 1.7m * _wholesalePrice * _shippingCost;
}
}
public decimal _wholesalePrice
{
get
{
return _wholesalePrice;
}
set
{
//Retail price is partially determined by wholesale price. Make sure retail price stays up to date.
_retailPrice = 1.7m * _wholesalePrice * _shippingCost;
}
}
public int _quantity { get; set; }
public decimal _shippingCost { get; private set; }
public decimal _retailPrice { get; private set; }
public Item(string name, double weight, decimal wholesalePrice, int quantity) : this()
{
_name = name;
_weight = weight;
_wholesalePrice = wholesalePrice;
_quantity = quantity;
}
//More stuff
我在另一个类中也有一个 Item 实例。当我尝试通过以下命令调用 weight 属性时,程序崩溃:
currentUIItem._weight = formattedWeight;
未提供描述性错误。请注意,此时, currentUIItem 已使用无参数默认构造函数进行了更新。现在,这是奇怪的部分。当我删除 weight 的 set 属性的自定义实现并将其替换为通用 { get; 放; },作业完美无缺。
有谁知道这里发生了什么?这是一个可以在类中正常工作的结构怪癖吗?