Overload resolution priority on extension methods #9337
Unanswered
jhudsoncedaron
asked this question in
General
Replies: 1 comment 2 replies
-
|
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This one is actually ambiguous in the specification at https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-13.0/overload-resolution-priority ; the problem is there is not one specification for the whole language so it depends on which order you read specification components.
Anyway this problem has been a thorn in our side for awhile; There's an extension method on IDictionary<,> called GetValueOrDefault() which is convenient; and we added another IReadOnlyDictionary<,> called GetValueOrDefault() for the same reason the the stock one was added. Guess what happens when someone tries to call Dictionary.GetValueOrDefault()
When I read the specification for overload resolution priority https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-13.0/overload-resolution-priority ; unless I read it in the mode of how can the specification be twisted to make the most trouble possible, I conclude placing
[OverloadResolutionPriority(-1)]
on my method should mask it and allow Dictionary<,>.GetValueOrDefault() to resolve to the stock one.I can't rule out the possibility that we were there first and Microsoft is the one who added the colliding name. I am not the one who made this method.
Sample project is attached. In the production version it wouldn't matter which function is called.
x.zip
Beta Was this translation helpful? Give feedback.
All reactions