102

Can somebody provide a example of this?

I have tried null,string.Empty and object initialization but they don't work since default value has to be constant at compile time

0

7 Answers 7

173

Just use the null coalescing operator and an instance of empty List<string>

public void Process(string param1, List<string> param2 = null) 
{
    param2 = param2 ?? new List<string>();

    // or starting with C# 8
    param2 ??= new List<string>();
}

The problem with this is that if "param2" is null and you assign a new reference then it wouldn't be accessible in the calling context.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes,I get the limitation but this worked well for me in this case.
40

You may also do the following using default which IS a compile-time-constant (null in the case of a List<T>):

void DoSomething(List<string> lst = default(List<string>)) 
{
    if (lst == default(List<string>)) lst = new List<string>();
}

You can simplify this even further to:

void DoSomething(List<string> lst = default) 

1 Comment

IntelliSense is showing me the statement can be simplified even further by just using "default" instead of "default(List<string>").
6

It is impossible. You should use method overloading instead.

public static void MyMethod(int x, List<string> y) { }
public static void MyMethod(int x)
{
    MyMethod(x, Enumerable<string>.Empty());
}

2 Comments

It is impossible to have a non-compile time constant, such as an empty list, as the default value. I know that you can use default values for parameters in C#, obviously...
However, this would be the only solution or anyone using a C# version before 4.0.
6

As others mentioned you assign null to the optional parameter, in newer versions when using <Nullable>enable</Nullable> you need to mark the parameter with the nullable annotation (?) and assign a null value to it, otherwise it will cause error CS8625 - Cannot convert null literal to non-nullable reference type.

void DoSomething(string param, List<string>? optional = null)
{
   // Check if the parameter is null, if so create empty list
   optional ??= new();

   ...
} 

Comments

3
    private void test(List<string> optional = null)
    {

    }

sorry about the string instead of list. Null works fine for me on 4.0, i am using visual studio 2010

4 Comments

That's string, not List<string>
It compiles ,but you get Object Instantiation exception at run time
I did not get that exception, make sure you test your answers before making the claim.
Works fine for me, but needs to be at the end if you have multiple parameters
-1

I like it this way, it is more readable then ??=

if (param == null) param = new();

Comments

-3
private void test(params object[] params)
{

}

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.