Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Implicit interface implementation has a requirement that the generic constraints on the method declarations be equivalent, but not necessarily exactly the same in code. Additionally, generic type parameters have an implicit constraint of "where T : object". That is why specifying <code>C&lt;Object&gt;</code> compiles, it causes the constraint to become equivalent to the implicit constraint in the interface. (Section 13.4.3 of the <a href="http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&amp;FamilyID=dfbf523c-f98c-4804-afbd-459e846b268e" rel="nofollow">C# Language Spec</a>).</p> <p>You're also correct that using an explicit interface implementation that calls into your constrained method will work. It provides a very clear mapping from the interface method to your implementation in the class where the constraints <em>cannot</em> differ, and then proceeds to call a similarly-named generic method (one that now has nothing to do with the interface). At that point, constraints on the secondary method can be resolved in the same way as any generic method call without any interface resolution issues.</p> <p>Moving the constraints from the class to the interface, in your second example, is better because the class will take its constraints from the interface by default. This also means that you must specify the constraints in your class implementation, if applicable (and in the case of Object it is not applicable). Passing <code>I&lt;string&gt;</code> means that you can't directly specify that constraint in code (because string is sealed) and so it must either be part of an explicit interface implementation or a generic type that will be equal to the constraints in both places.</p> <p>As far as I know, the runtime and the compiler use separate verification systems for constraints. The compiler allows this case but the runtime verifier doesn't like it. I want to stress that I don't know for sure why it has a problem with this, but I would guess that it doesn't like the potential in that class definition to <em>not</em> fulfill the interface constraints depending on what T ends up being set to. If anyone else has a definitive answer on this, that would be great.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload