c# - 如何在 c# 的 setter 中设置值
问题描述
我想添加自定义逻辑,如果它是 0(默认),我的 ulong 将设置为 1。这是我所拥有的:
private ulong quantity;
public ulong Quantity
{
get
{
return this.quantity;
}
set
{
if (this.Quantity == 0)
{
this.quantity = 1;
return;
}
this.Quantity = this.quantity;
}
}
但是,我收到一个编译错误,上面写着:
Parameter 'value' of 'PurchaseForPayoutRequest.Quantity.set(ulong)' is never used
解决方案
您需要在 setter 中使用上下文关键字 value
。
public ulong Quantity
{
get
{
return this.quantity;
}
set
{
if (value == 0)
{
this.quantity = 1;
return;
}
this.quantity = value;
}
}