Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:02:55+00:00 2026-06-15T15:02:55+00:00

Possible Duplicate: Can’t operator == be applied to generic types in C#? I have

  • 0

Possible Duplicate:
Can’t operator == be applied to generic types in C#?

I have a “GenericNode”-parent/child structure that I’m trying to make a Find method for.

public class GenericNode<TKey, TName>
{
  private GenericNode<TKey, TName> parent;
  private readonly TKey key;
  private readonly TName name;
  private readonly ICollection<GenericNode<TKey, TName>> children;

  public GenericNode(TKey key, TName name) {
    this.key = key;
    this.name = name;
    children = new Collection<GenericNode<TKey, TName>>();
  }

  // AddChild etc...

  public GenericNode<TKey, TName> GetChildNodeById(TKey keyToFind)
  {
    return FindChild(node => node.key == keyToFind); // <--- THIS WON'T COMPILE
  }

  private GenericNode<TKey, TName> FindChild(Func<GenericNode<TKey, TName>, Boolean> matcher)
  {
    // Recursive search returning first matching node...
  }
}

The above (C# .NET 3.5) doesn’t work because it can’t compare node.key to keyToFind in the GetChildNodeById-method. I’ve been trying different generic constraints etc without luck. How can I specify that the TKey:s are the same type?

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:02:56+00:00Added an answer on June 15, 2026 at 3:02 pm

    There are a lot of ways to achieve this:

    1. Make a constraint so that TKey : IComprabale, then in GetNodeByKey you can call Compare on the key.

    2. Make a constraint so that TKey : IEquatable<TKey>, then you can call Equals on the key.

    3. Instead of using the equality operator, call Equals on the key. Note that you risk a NullReferenceException that way, and that in order to check the key for null you need to add a constraint TKey : class.

    4. Call Object.Equals on the keys. Note that this has a performance hit if your keys are value types, because in the call to Object.Equals they will be boxed.

    5. Let the user of the tree class provide his comparer. I would recommend adopting the pattern of standard .NET collections. For example look at the different constructors of Dictionary that can take a comparer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can’t operator == be applied to generic types in C#? I have
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've coded
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've got
Possible Duplicate: pthread Function from a Class I have this code that I can't
Possible Duplicate: Absolute Beginner's Guide to Bit Shifting? anyone can explain me that operator
Possible Duplicate: Can I overload an == operator on an Interface? I do understand
Possible Duplicate: Java operator overload In c++, we can perform the operator overloading. But
Possible Duplicate: Operator[][] overload I've looked on the internet but can't find a definitive
Possible Duplicate: Can I scroll a ScrollView programmatically in Android? I have a chat
Possible Duplicate: C# generic constraint for only integers As you can see in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.