I know this can be done using null so I have a workaround for that, but I was wondering if there was a better way that I can have an optional int[] parameter for a class?
class PriceLevels
{
public int[] priceLevels { get; }
private readonly int[] defaultPriceLevels = { 2, 3, 3, 4, 5, 6 };
public PriceLevels(int[] newPriceLevels = defaultPriceLevels)
{
priceLevels = newPriceLevels;
}
}
This gives me an error saying it is an invalid expression defaultPriceLevels must be constant. How can I fix this?
One workaround that I can thing of is this, but I don't really like the solution
class PriceLevels
{
public int[] priceLevels { get; }
private readonly int[] defaultPriceLevels = { 2, 3, 3, 4, 5, 6 };
public PriceLevels(int[] newPriceLevels = null)
{
if(newPriceLevels == null)
priceLevels = defaultPriceLevels;
else
priceLevels = newPriceLevels;
}
}
priceLevelsproperty has no setter?priceLevelsto be set on initialization (hence the default value as a fallback), then it's valid.