Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>public override List&lt;IKPIDetails&gt; MakeReportDataStructure() { List&lt;AirTemp_KPIDetails&gt; d = new List&lt;AirTemp_KPIDetails&gt;(); return d; // &lt;-- this is where is breaks } </code></pre> <p>Should be:</p> <pre><code>public override List&lt;IKPIDetails&gt; MakeReportDataStructure() { List&lt;IKPIDetails&gt; d = new List&lt;AirTemp_KPIDetails&gt;(); return d; } </code></pre> <p>Your assignment does not work, because in <code>List&lt;T&gt;</code>, type argument <code>T</code> is not covariant, that is, assignment is not allowed when <code>T</code> is more specific. Should you use <code>IEnumerable</code>, your code would work fine:</p> <pre><code>public override IEnumerable&lt;IKPIDetails&gt; MakeReportDataStructure() { List&lt;AirTemp_KPIDetails&gt; d = new List&lt;AirTemp_KPIDetails&gt;(); return d; // &lt;-- works, because T in IEnumerable is covariant } </code></pre> <p>You may ask, why <code>IEnumerable</code> is covariant? Well, <code>IEnumerable</code> only allows you to read data (hence covariance is marked with <code>&lt;out T&gt;</code>), and you cannot write any object into it. List allows you to write objects into collection, and you would run into danger that you would later write less specific type to that collection, which could cause error, as less specific type does not provide same functionality as more derived one:</p> <pre><code>public override IEnumerable&lt;BaseClass&gt; MakeList() { List&lt;DerivedClass&gt; d = new List&lt;DerivedClass&gt;(); // ... return d; // assume it would work } var l = MakeList(); l.Add(BaseClass); // now we added object of BaseClass to the list that expects object of DerivedClass, any operation on the original list (the one with List&lt;DerivedClass&gt; type could break </code></pre>
 

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