Note that there are some explanatory texts on larger screens.

plurals
  1. POC# generic implicit cast on Interface failed
    primarykey
    data
    text
    <p>Why will the below not compile? What's special about the interface that causes the compiler to think it can't cast from <code>Container&lt;T&gt;</code> to <code>T</code>, when <code>T</code> is an interface? I don't think its a covariant issue, as I'm not downcasting, but perhaps it is. This is quite like <a href="https://stackoverflow.com/questions/393701/why-c-compiler-doesnt-call-implicit-cast-operator">Why C# compiler doesn't call implicit cast operator?</a> but I don't think it's quite the same.</p> <pre><code>Product pIn =null; Product pOut; Container&lt;Product&gt; pContainer; List&lt;Product&gt; pListIn = null; List&lt;Product&gt; pListOut; Container&lt;List&lt;Product&gt;&gt; pListContainer; IList&lt;Product&gt; pIListIn = null; IList&lt;Product&gt; pIListOut; Container&lt;IList&lt;Product&gt;&gt; pIListContainer; pContainer = pIn; pOut = pContainer; // all good pListContainer = pListIn; pListOut = pListContainer; // all good too pIListContainer = pIListIn; // fails , cant do implicit cast for some reason pIListOut = pIListContainer; // and here too </code></pre> <hr> <pre><code>class Container&lt;T&gt; { private T value; private Container(T item) { value = item; } public static implicit operator Container&lt;T&gt;(T item) { return new Container&lt;T&gt;(item); } public static implicit operator T(Container&lt;T&gt; container) { return container.value; } } </code></pre> <hr> <pre><code>Cannot implicitly convert type 'Container&lt;IList&lt;Product&gt;&gt;' to 'IList&lt;Product&gt;'. An explicit conversion exists (are you missing a cast?) Cannot implicitly convert type 'IList&lt;Product&gt;' to 'Container&lt;IList&lt;Product&gt;&gt;'. An explicit conversion exists (are you missing a cast?) </code></pre>
    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.
 

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