Overloading methods that accepts delegates which differs in nullability #7525
-
class Indexer<T>
{
void Index(Func<T, string> selector)
{}
void Index(Func<T, string?> selector)
{}
} The above code does not compile due to the following error:
However, I want to determine whether selector selects a nullable value or not. I could use different names instead of overloading but that would negatively impact the library user experience. Is there any other solutions to this besides avoiding overloading? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
You can define your own delegate types, but if these are simple delegates along the lines of |
Beta Was this translation helpful? Give feedback.
-
Well, the answers above should address your question but even if this was possible any particular reason this is desired? I mean it might worth considering different names. |
Beta Was this translation helpful? Give feedback.
@HazyFish yes you are right of course, with the main usage scenario being supplying a lambda/delegate, the custom delegate types don't work, either.
Looking at your snippet now, I tend to agree with @iam3yal that a different name is in order here, because the difference is so subtle that you want to make sure users know what they are calling - even if C# supported overloading on nullability (which it doesn't).