0

Hi C# experts out there

I have the following C# constructor which all have default values but I cannot figure out a way to set a meaningful default value for the double[] signal parameter:

public Raw(double amplitude = 10.0, ulong start = 0ul, ulong end = 10ul, double[] signal = null) : base(amplitude, start, end)
{
    if (signal == null)
    {
       this.Signal = new double[0];
    } else
    {
       this.Signal = signal;
    }
}

Is it even possible to use anything else besides null? What I actually want is an empty array like I do currently in the constructor's body. If I use new double[0] as default value the compiler complains that the parameter value must be compile-time constant. Any other approach to solve the above-shown scenario besides my current approach?

Thanks in advance for your support :)

4
  • Why don’t you just provide a method overload without that parameter? Commented Oct 30, 2018 at 15:38
  • 2
    BTW, your ctor code can be simplified: this.Signal = signal ?? new double[0]; Commented Oct 30, 2018 at 15:39
  • default parameter value of a reference type other than string can only be initialized with null Commented Oct 30, 2018 at 15:41
  • ok thanks for the info :) Commented Oct 30, 2018 at 15:48

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.