所以我有这个类Battery和Enum BatteryType:
public enum BatteryType { LiIon, NiMH, NiCd };
public class Baterry
{
public string Model { set; get; }
public int HoursIdle { set; get; }
public int HoursTalk { set; get; }
private BatteryType BatteryType;
public Baterry(string model) : this(model, 0)
{
}
public Baterry(string model, int hoursidle) : this(model, hoursidle, 0, null)
{
}
public Baterry(string model, int hoursidle, int hourstalk, BatteryType batteryType)
{
this.Model = model;
this.HoursIdle = hoursidle;
this.HoursTalk = hourstalk;
this.BatteryType = batteryType;
}
}
问题是
public Baterry(string model, int hoursidle) : this(model, hoursidle, 0, null)
{
}
我得到方法获取无效的参数错误。我该如何解决?
最佳答案
在C#中,枚举不是可为空的类型,因此不能将BatteryType
设置为null
。为您的枚举找到默认值或使您的枚举可为空:
private BatteryType? batteryType;
如果您只想使用
0
作为默认枚举值,则不必设置任何内容,因为0
仍然是枚举的隐式默认值。