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 8740099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:04:20+00:00 2026-06-13T11:04:20+00:00

I have a List < MyObj > I have also overriden Equals(Object) in class

  • 0

I have a List < MyObj >

I have also overriden Equals(Object) in class MyObj.

When I call indexof on my list it does not get any index and equals is not called.

Is my assumption correct that Equals(Object) should be called when I call index of ?

If not how do I find MyObj objects if I do not have exact reference but objects are logically same,,,?

here is the code I am using

 public class TreeNode<T>
    {
        private readonly List<TreeNode<T>> _children = new List<TreeNode<T>>();     

        public TreeNode(T value)
        {
            Value = value;
        }

        public virtual bool Equals(Object obj)
        {
            TreeNode<T> treeNode = (TreeNode<T>)obj;

            if (treeNode.Value.Equals(Value))
                return true;
            else
                return false;
        }


        public TreeNode<T> this[int i]
        {
            get { return _children[i]; }
        }

        public TreeNode<T> Parent { get; private set; }
        public T Value { get; set; }

        public ReadOnlyCollection<TreeNode<T>> Children
        {
            get { return _children.AsReadOnly(); }
        }

        public TreeNode<T> AddChild(T value)
        {
            var node = new TreeNode<T>(value) { Parent = this };
            _children.Add(node);
            return node;
        }

        public bool RemoveChild(TreeNode<T> node)
        {
            return _children.Remove(node);
        }

        public void Traverse(Action<T> action)
        {
            action(Value);
            foreach (var child in _children)
                child.Traverse(action);
        }


    }

this is how I am call the collection

  int artistIndex = serverDirs.Children.IndexOf(new TreeNode<String>(artist));

in the index returned in always -1 even though the item is there in the children
Thanks,

  • 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-13T11:04:21+00:00Added an answer on June 13, 2026 at 11:04 am

    Look in the Error List window after you rebuild your program:

    warning CS0114: ‘ConsoleApplication1.TreeNode.Equals(object)’ hides inherited member ‘object.Equals(object)’. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
    c:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll: (Related file)

    What this is telling you is that your Equals() method does not in fact override the Equals() method that the equality comparer is going to use. Which is why you don’t see it being called and why you get a -1 return from IndexOf(). Fix:

        public override bool Equals(Object obj) {        // NOTE: override, not virtual
            TreeNode<T> treeNode = obj as TreeNode<T>;
            if (treeNode == null) return false;
            return treeNode.Value.Equals(Value);
        }
    

    This fixes your problem, albeit that you now get a new warning:

    warning CS0659: ‘ConsoleApplication1.TreeNode’ overrides Object.Equals(object o) but does not override Object.GetHashCode()

    Don’t ignore that, it’s going to byte sooner or later. Fix:

        public override int GetHashCode() {
            return Value.GetHashCode();
        }
    

    The lesson to learn from this: don’t ignore warnings. The compiler is often correct when it flags something smelly. And feel free to blame the IDE, it doesn’t show the Error List window when there are only warnings. Sloppy.

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

Sidebar

Related Questions

I have a List<MyObj> with the class MyObj : IComparable . I wrote the
In C#, if I have a List<MyObj> where MyObj is a custom class with
Say I have the following java code: Object myObj = new ArrayList(); List myList
I have List where myObj has it own list of objects called example mySubObj.
I have an object that contains public class PositionsChannelApplicationGroups { public PositionsChannelApplicationGroups(){} private Map<MyObj1,
Say I have a List of objects, and the object has a string property.
I have a generic list: IList<T> myobj = new List<T>(); how to check if
In the following situation, how does the parameter-list for sortMyVectors have to look like,
I have: Dim aList As List(Of myObj.Answers) = myObj.GetResults(someID) This returns: aList.AnswerID | aList.AnswerCount
I have an interface with a method public List< Object > getLeftBusinessObjects( List< Object

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.