0

I have a method that takes a string[] as parameter, but I am unable to provide it with a default:

void Foo(string[] param = new[]) {
}

complains that the default should be a compile time constant. But what compile time constant is there as an empty array?

I know I can use default, but I was wondering what the syntax was explicitly, if at all possible.

5
  • You can use null to represent an empty array. What kind of default value do you actually need ? Commented Jan 28, 2023 at 12:17
  • @wohlstad an empty array. I don't like null and prefer Null-objects. Commented Jan 28, 2023 at 12:17
  • @BartFriederichs A default value must be constant expression, an expression of valuetype, an expression of default(valuetype). So only option is to set it to null for string[] Commented Jan 28, 2023 at 12:19
  • 1
    @Tej or use default(string[]) which is pretty much the same as using null. dotnetfiddle.net/TRXdny Commented Jan 28, 2023 at 12:22
  • You could use a params array, which is an empty array by default in case no actual args are provided void Foo(params string[] myArr) Commented Jan 28, 2023 at 13:57

3 Answers 3

1

A default value must be compile time value and there are no compile time const arrays in C# and your options are:

  1. don't have a default value
  2. use null/default

default will mean null so you'd have to change string[] to string[]?.

void Foo(string[]? x = default) // OK. 
{
}


void Foo(string[] x = default) // Cannot convert null literal to non-nullable reference type.
{
}

To soften the blow you could:

void Foo(string[]? x = default)  
{
   string[] y = x ?? new string[] {};

   // use y below
}

or even:

void Foo() => Foo(new string[] {});

void Foo(string[] a) 
{
}
Sign up to request clarification or add additional context in comments.

2 Comments

string[] param = null works fine, as does string[] param = default
string[] a = null works with nullable reference types off which in practice is <.NET 6 (C# 10). You said "I don't like null" so I assumed you have nullable reference types on.
0

You can use an optional parameter with the params keyword which allows you to pass a variable number of arguments to the method.

You can refer to this page. https://geeksnewslab.com/how-to-add-a-default-value-for-a-string-array-in-a-method/

2 Comments

In future questions consider adding some portion of the linked code so in case the link goes dead your answer is still useful. I have added a link to the params keyword documentation to mitigate that chance.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

The default value must be one of the following in C# :

  1. A constant expression;

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

  3. An expression of the form default(ValType), where ValType is a value type."

The arrays you created don't follow any of the above rules of C#, and it is a reference type so there can be only one default value which is: null

And one more thing, Array is a reference type, so it initializes by new keyword so anything which initializes by new cannot be constant thus constant of the reference type is null

So You can use = null or = default

void Foo(string[] param = null) {
  param = param==null ? new[] : param;
}

OR

void Foo(string[] param = default) {
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.