Generic type parameter and Nullable method overload

核能气质少年 提交于 2019-11-30 13:03:12

Constraints within the generic method are not considered while choosing an overload - they're checked after the overload has been chosen.

Constraints within the types of the parameters are checked as part of choosing an overload. It's a bit confusing, but it makes sense eventually.

I have a blog post on this which may help to understand it further.

Additionally note that your second example has the additional argument which contributes to type inference, which is what makes the difference between the two. TResult is inferred to be int, which prevents the first overload from being valid - there's no conversion from (int? x) => x + 1 to Func<int?, int> whereas there is a conversion from (int x) => x + 1 to Func<int, int>.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!