Optional Method Arguments on byte array
Asked Answered
K

4

5

How would i make a default value for a byte array argument? The code below wont work...

public static void init(SomeByteArray[] = {1, 2, 3, 4}) {
    //Do Something
}

Is this possible?

Im doing this in .Net Micro Framework 4.1, if it makes any difference...

Karissakarita answered 9/6, 2013 at 18:55 Comment(1)
Unfortunately, you can't, as all optional arguments need to be compile-time constants.Shakedown
B
6

From MSDN:

A default value must be one of the following types of expressions:

  • a constant expression;
  • an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct;
  • an expression of the form default(ValType), where ValType is a value type.

So an array instance cannot be used as default value.

The best solution is probbaly two define two overloads as follows:

public static void Init()
{
    Init(new byte[] { 1, 2, 3, 4 });
}

public static void Init(byte[] data)
{
    ...
Brittan answered 9/6, 2013 at 19:3 Comment(1)
This also has the slight advantage that you can make it invalid for the caller to pass a null to init(), while still having the ability to use a default by omitting the parameter. I'd definitely go with this one myself.Axseed
U
4

You can, but it needs to be assigned in the method, and the default value has to be null, like this:

public static void init(byte[] SomeByteArray = null)
{
    SomeByteArray = SomeByteArray ?? new byte[] {1, 2, 3, 4};
    //carry on with your method.
}
Urbanity answered 9/6, 2013 at 19:1 Comment(1)
You need to specify byte explicitly as new bye[] {1, 2, 3, 4}, or it's treated as an int array. The peers rejected my revision for that.North
L
3

You can't do that. Default parameters must be compile-time constants, and unfortunately arrays are not.

However, you could do this:

public static void init(byte[] SomeByteArray = null) {
    SomeByteArray = SomeByteArray ?? new byte[] { 1, 2, 3, 4 };
    // Do Something
}

Or this:

public static void init() {
    init(new byte[] { 1, 2, 3, 4 });
}

public static void init(byte[] SomeByteArray) {
    // Do Something
}
Lonergan answered 9/6, 2013 at 18:59 Comment(6)
@newStackExchangeInstance oops, yes, exactlyLonergan
Would be the same with init(byte[] SomeByteArray)Lu
@Lu No it wouldn't, because you'd have to type in null then.Shakedown
I would personally suggest the same code as the former shows.North
I get "operator ?? cannot be applied to operands of type byte[] and int[]"Karissakarita
@ChristianBekker Oops, I guess you need to specify the array type in this case. See my updated answer.Lonergan
R
0

Try:

public class MyClass {
    private readonly int[] _default = new[] { 1, 2, 3, 4 }; 

    public static void init(int[] myArray = null) {
        if (myArray == null) {
            // Don't modify _default to ensure thread safe
            myArray = _default;
        }

        //Do Something
    } 
} 
Rill answered 9/6, 2013 at 18:58 Comment(2)
You got it wrong there, you're assigning null instead of checking if it is null. Fixing it for you :)Shakedown
Thanks @Ken and newStackExchangeInstance - whoops!Rill

© 2022 - 2024 — McMap. All rights reserved.