Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy cannot C# generics derive from one of the generic type parameters like they can in C++ templates?
    primarykey
    data
    text
    <p>Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates? I mean I know it impossible because CLR does not support this, but why?</p> <p>I am aware of the profound differences between C++ templates and C# generics - the former are compile time entities and must be resolved during the compilation, while the latter are first class run-time entities.</p> <p>Still, I am failing to see <strong>the reason</strong> why CLR designers did not come up with a scheme which would ultimately enable a CLR generic type to derive from one of its generic type parameters. After all, this would be tremendously useful feature, I personally miss it greatly.</p> <p><strong>EDIT:</strong></p> <p>I would like to know of a hard-core issue, fixing which yields such a high price on implementing this feature that justifies it not being implemented yet. For instance, examine this fictional declaration:</p> <pre><code>class C&lt;T&gt; : T { } </code></pre> <p>As Eric Lippert has noticed what if "<strong>What if T is a struct? What if T is a sealed class type? What if T is an interface type? What if T is C?! What if T is a class dervied from C? What if T is an abstract type with an abstract method? What if T has less accessibility than C ? What if T is System.ValueType? (Can you have a non-struct which inherits from System.ValueType?) What about System.Delegate, System.Enum, and so on?</strong>"</p> <p>As Eric continues, "<strong>Those are the easy, obvious ones</strong>". Indeed, he is right. I am interested in a concrete example of some neither easy nor obvious issue, which is hard to resolve.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    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