A tricky bit of code
I run into the following bit of code while doing a code review on a pull request:
This was very strange, because the code appeared to compile properly, but it shouldn’t. I mean, look at it. The generic parameter is not constrained, and I don’t have any extension methods on Object that can apply here, so why would this compile?
The secret was in the base class:
Basically, we specified the constraint on the abstract method, and then inherited it, which was really confusing to me until I figured it out.
You can’t do the same with interfaces, though, although explicit interface implementation does allow it.
Comments
Open, as usual even a 5 line post from you makes me think... So first of all that you! Could you share the review notes on this? What would be a more readable way?
Thanks again!
*Oren... 😟
Janivz, What do you mean?
Janivs, Oren states that the code piece seems strange yet it compiles fine. It seems strange because at first glance there is no way you would know type "T" has a method "Add".
I think I was the one that caused that :D ... I didnt understand the error message myself where it didnt allow me to add the constraints to the override method.
Comment preview