Replies: 1 comment 2 replies
-
Generally, overload resolution is a language topic and should go to https://github.com/dotnet/csharplang/discussions. Different languages can have different rules.
This is an incorrect expectation. It's often encouraged to provide shorter overloads with less parameters to make an optimal path that don't need to deal with optional parameters.
You can use Visual Studio to get assisted about overload resolution. Visual Studio runs the actual compiler and would highlight/navigate you to the selected overload.
The full spec of overload resolution is at https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#1264-overload-resolution. Optional parameter is a very old feature so there wouldn't be fresh design notes.
People don't encounters this as ambiguity. Exact match always wins over anything else. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Background:
In C#, consider the following method overloads:
From my understanding, method overloads should have distinguishable parameter signatures, and relying only on optional parameters for distinction can lead to ambiguity. I expected that such definitions would result in a compile-time error due to ambiguous overload resolution, especially when calling
Test("")
.However, in practice, I have noticed that sometimes the compiler does not report an error for such code.
My questions are:
Thank you for your help and clarification!
Beta Was this translation helpful? Give feedback.
All reactions