Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>C# doesn't support generic collections covariance (it's only supported for arrays). I use an adapter class in such cases. It just redirects all calls to the actual collection, converting values to the required type (doesn't require copying all list values to the new collection). Usage looks like this:</p> <pre><code>Collection&lt;INail&gt; IBucket.Nails { get { return new ListAdapter&lt;Nail, INail&gt;(nails); } } // my implementation (it's incomplete) public class ListAdapter&lt;T_Src, T_Dst&gt; : IList&lt;T_Dst&gt; { public ListAdapter(IList&lt;T_Src&gt; val) { _vals = val; } IList&lt;T_Src&gt; _vals; protected static T_Src ConvertToSrc(T_Dst val) { return (T_Src)((object)val); } protected static T_Dst ConvertToDst(T_Src val) { return (T_Dst)((object)val); } public void Add(T_Dst item) { T_Src val = ConvertToSrc(item); _vals.Add(val); } public void Clear() { _vals.Clear(); } public bool Contains(T_Dst item) { return _vals.Contains(ConvertToSrc(item)); } public void CopyTo(T_Dst[] array, int arrayIndex) { throw new NotImplementedException(); } public int Count { get { return _vals.Count; } } public bool IsReadOnly { get { return _vals.IsReadOnly; } } public bool Remove(T_Dst item) { return _vals.Remove(ConvertToSrc(item)); } public IEnumerator&lt;T_Dst&gt; GetEnumerator() { foreach (T_Src cur in _vals) yield return ConvertToDst(cur); } IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } public override string ToString() { return string.Format("Count = {0}", _vals.Count); } public int IndexOf(T_Dst item) { return _vals.IndexOf(ConvertToSrc(item)); } public void Insert(int index, T_Dst item) { throw new NotImplementedException(); } public void RemoveAt(int index) { throw new NotImplementedException(); } public T_Dst this[int index] { get { return ConvertToDst(_vals[index]); } set { _vals[index] = ConvertToSrc(value); } } } </code></pre>
    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.
    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