1

E.g, I've got a member function like this:

void f(List<int> listInt)...

Can I specify a default value for this listInt? Seems I cannot directly give it a "new" value as compiler requires a compile time constant for default values.

How to achieve that?

Thanks a lot.


Note I'm using Vs2017 and dotnet core 2.0, while this thread [C#-How to use empty List as optional parameter] is too old and the solution no longer works.

Troskyvs
  • 7,537
  • 7
  • 47
  • 115
  • According to [Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments), this cannot be done, although you can easily bypass the problem as explained in the duplicate answers – Steve Mar 02 '20 at 07:52
  • I reopened the question, but according to my test with LInqPAD6 (Net Core version 3.1) it works as expected using the default(List) approach – Steve Mar 02 '20 at 08:10
  • The only default value you can use in the signature for `List` is `null`. Inside the method you can replace `null` with whatever you want, but in the signature you only have one option. – Lasse V. Karlsen Mar 02 '20 at 08:13
  • @Steve Why was the question reopened? The duplicate in the edit history is the only option and answers this question perfectly. Was there some confusion about what the issue is? – Lasse V. Karlsen Mar 02 '20 at 08:26
  • @LasseV.Karlsen The OP added the info about using VS2017 and net core 2.0. I am not able to test if something different in that configuration (I strongly doubt about it). Cannot close again now. – Steve Mar 02 '20 at 09:29

0 Answers0