Allow implicit conversions in default values for optional parameters #1538
Replies: 6 comments
-
This code is not enough? public static void AddCategory(Union<string, string[]> cat = null)
{
cat = cat ?? "none";
} |
Beta Was this translation helpful? Give feedback.
-
Short answer: no. Long answer: |
Beta Was this translation helpful? Give feedback.
-
@ufcpp : Is this still a good pattern considering the upcoming nullable-reference-type feature in C# 8.0? I have also done your solution multiple times in the past, but I never considered it very clean, so I usually made two methods overloading each other, e.g.: public static void AddCategory() => AddCategory("none");
public static void AddCategory(Union<string, string[]> cat)
{
// ...
} |
Beta Was this translation helpful? Give feedback.
-
AFAIK, the only legal default value for reference types (except To support this scenario, two rather unlikely things would have to occur:
|
Beta Was this translation helpful? Give feedback.
-
@stakx, You can change the Union<,> to a struct and you still have the problem. I created the issue to discuss if limitations for default values can be relaxed (if "the only legal default value for reference types (except string) is null" can be relaxed, it would be nice too). |
Beta Was this translation helpful? Give feedback.
-
@emumanu: The same restrictions I mentioned above apply to |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I have this class:
And I have a method like this:
I want it to work, but currently it is giving me the following error:
Error CS1750 A value of type 'string' cannot be used as a default parameter because there are no standard conversions to type 'Union<string, string[]>'
It would be nice to support that case.
Thank you
Beta Was this translation helpful? Give feedback.
All reactions