Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Generic Class with "specialized" constructor
    text
    copied!<p>I have a class like the following:</p> <pre><code>public class DropDownControl&lt;T, Key, Value&gt; : BaseControl where Key: IComparable { private IEnumerable&lt;T&gt; mEnumerator; private Func&lt;T, Key&gt; mGetKey; private Func&lt;T, Value&gt; mGetValue; private Func&lt;Key, bool&gt; mIsKeyInCollection; public DropDownControl(string name, IEnumerable&lt;T&gt; enumerator, Func&lt;T, Key&gt; getKey, Func&lt;T, Value&gt; getValue, Func&lt;Key, bool&gt; isKeyInCollection) : base(name) { mEnumerator = enumerator; mGetKey = getKey; mGetValue = getValue; mIsKeyInCollection = isKeyInCollection; } </code></pre> <p>And I want to add a convenience function for Dictionaries (because they support all operations efficiently on their own).</p> <p>But the problem is that such a constructor would only specify Key and Value but not T directly, but T is just KeyValuePair. Is there a way to tell the compiler for this constructor T is KeyValuePair, like:</p> <pre><code>public DropDownControl&lt;KeyValuePair&lt;Key, Value&gt;&gt;(string name, IDictionary&lt;Key, Value&gt; dict) { ... } </code></pre> <p>Currently I use a static Create function as workaround, but I would like a direct constructor better.</p> <pre><code>public static DropDownControl&lt;KeyValuePair&lt;DKey, DValue&gt;, DKey, DValue&gt; Create&lt;DKey, DValue&gt;(string name, IDictionary&lt;DKey, DValue&gt; dictionary) where DKey: IComparable { return new DropDownControl&lt;KeyValuePair&lt;DKey, DValue&gt;, DKey, DValue&gt;(name, dictionary, kvp =&gt; kvp.Key, kvp =&gt; kvp.Value, key =&gt; dictionary.ContainsKey(key)); } </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