Note that there are some explanatory texts on larger screens.

plurals
  1. POAlter elements of generic collection property in a subclass child collection
    primarykey
    data
    text
    <p>I have a simple interface ElementContainer (which is also an element) which holds a list with Elements in it:</p> <pre><code>interface IElementContainer : IElement { List&lt;Element&gt; Elements { get; } } </code></pre> <p>Now I have a class implementing this interface:</p> <pre><code>class ConcreteContainer : IElementContainer { List&lt;IElement&gt; Elements { get; private set; } } </code></pre> <p>But I don't want the class to be a container for all Elements but only for a subset of them, say SpecificElement, however this is not possible (ofcourse):</p> <pre><code>class SpecificElementContainer : IElementContainer { List&lt;SpecificElement&gt; Elements { get; private set; } } </code></pre> <p>Therefore I thought of something like:</p> <pre><code>class SpecificElementContainer : IElementContainer { List&lt;IElement&gt; Elements { get; private set; } List&lt;SpecificElement&gt; SpecificElements { get { return return Elements.FindAll(el =&gt; el is T).Select&lt;IElement, T&gt;(el =&gt; (T)el).ToList&lt;T&gt;(); } } </code></pre> <p>But this way I can not do something like: <code>SpecificContainer.SpecificElements.Add(...)</code> since it not added to the Elements. And besides that... it smells fishy to do it like this.</p> <p>Any clues how to restructure this in a sane way?</p> <p><em><strong>Edit after Jon Skeet's answer</em></strong></p> <p>Reading Jons comment my problem is actually a little more complicated than I showed above but I forgot some very important information.</p> <p>When I have a class which implements the above interface and the elements list is filled like this:</p> <pre><code>Elements = { ConcreteElementA, ConcreteElementA, ConcreteElementC, ConcreteElementB } </code></pre> <p>What I would like is that I could do this <code>ConcreteContainer.ConcreteElementCs.Add(new ConcreteElementC())</code> which would result in:</p> <pre><code>Elements = { ConcreteElementA, ConcreteElementA, ConcreteElementC, ConcreteElementB, ConcreteElementC } </code></pre> <p><strong>Additional edit</strong></p> <p>After some confusion I want to rephrase my question as follows: I have a collection <code>List&lt;Element&gt; OriginalCollection</code>. I want to create a subset collection with elements which implement Element, ConcreteElement, <code>List&lt;ConcreteElement&gt; Subset</code> and add to that in such a way that <code>OriginalCollection</code> is also modified.</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.
 

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