Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding children in node structure using generic key?
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/390900/cant-operator-be-applied-to-generic-types-in-c">Can’t operator == be applied to generic types in C#?</a> </p> </blockquote> <p>I have a "GenericNode"-parent/child structure that I'm trying to make a Find method for.</p> <pre><code>public class GenericNode&lt;TKey, TName&gt; { private GenericNode&lt;TKey, TName&gt; parent; private readonly TKey key; private readonly TName name; private readonly ICollection&lt;GenericNode&lt;TKey, TName&gt;&gt; children; public GenericNode(TKey key, TName name) { this.key = key; this.name = name; children = new Collection&lt;GenericNode&lt;TKey, TName&gt;&gt;(); } // AddChild etc... public GenericNode&lt;TKey, TName&gt; GetChildNodeById(TKey keyToFind) { return FindChild(node =&gt; node.key == keyToFind); // &lt;--- THIS WON'T COMPILE } private GenericNode&lt;TKey, TName&gt; FindChild(Func&lt;GenericNode&lt;TKey, TName&gt;, Boolean&gt; matcher) { // Recursive search returning first matching node... } } </code></pre> <p>The above (C# .NET 3.5) doesn't work because it can't compare <code>node.key</code> to <code>keyToFind</code> in the <code>GetChildNodeById</code>-method. I've been trying different generic constraints etc without luck. How can I specify that the TKey:s are the same type?</p> <p>I could just create a derived class with a Guid for key and move the GetChildNodeById to that class but I would like a generic solution if possible. Any thoughts?</p>
 

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