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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:57:41+00:00 2026-05-19T13:57:41+00:00

I am using a System.Collections.Generic , which contains instances of a class I wrote.

  • 0

I am using a System.Collections.Generic, which contains instances of a class I wrote.

I have read that the collections .Contains method uses object.Equals(), or an implementation of the Equals() method from the IEquatable interface.

I have overridden the object method, as well as implemented from the interface. However, Queue.Contains(instance) is always returning false. What am I doing wrong?

For instance…

class Foo : IEquatable<Foo>
{
    ...
    int fooField1;
    ...

    public override bool Equals(object obj)
    {
         Foo other = obj as Foo;
         bool isEqual = false;

         if (other.fooField1 == this.fooField1)
         { 
             isEqual = true;
         }

         return isEqual;         
    }

    public bool Equals(Foo other)
    {
         bool isEqual = false;

         if (other.fooField1 == this.fooField1)
         { 
             isEqual = true;
         }

         return isEqual;         
    }

}
...

void SomeFunction()
{
    Queue<Foo> Q = new Queue<Foo>();
    Foo fooInstance1 = new Foo();
    Foo fooInstance2 = new Foo();

    fooInstance1.fooField1 = 5;
    fooInstance2.fooField1 = 5;

    Q.Enqueue(fooInstanec1);
    if(Q.Contains(fooInstance2) == false)
    {
         Q.Enqueue(fooInstance2);
    }
}

fooInstance2 is always added to the queue. In fact, when I run this on the debugger, the implementations of Equals are never reached.

What am I doing wrong?

  • 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-05-19T13:57:42+00:00Added an answer on May 19, 2026 at 1:57 pm

    Your sample code works as expected once the initial compile errors are sorted out. Not that it is relevant to the posed problem, Do read up on overriding Equals (you need to override GetHashCode too and check for error cases like null / type-mismatch).

    class Foo : IEquatable<Foo>
        {
            private int _fooField1;
            public Foo(int value)
            {
                _fooField1 = value;
            }
    
            public override bool Equals(object obj)
            {
                return Equals(obj as Foo);
            }
    
            public bool Equals(Foo other)
            {
                return (other._fooField1 == this._fooField1);
            }
        }
    
    
    
        class Program
        {
            static void SomeFunction()
            {
                var Q = new Queue<Foo>();
                Foo fooInstance1 = new Foo(5);
                Foo fooInstance2 = new Foo(5);
    
                Q.Enqueue(fooInstance1);
                if (!Q.Contains(fooInstance2))
                {
                    Q.Enqueue(fooInstance2);
                }
                else
                {
                    Console.Out.WriteLine("Q already contains an equivalent instance ");
                }
            }
    
            static void Main(string[] args)
            {
                SomeFunction();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface that contains a single method called ListAll() using System.Collections.Generic; namespace
I have such generic class of list: using System; using System.Collections; using System.Collections.Generic; using
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericCount { class Program {
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace EfTestFactory { public abstract class
I have this code (C#): using System.Collections.Generic; namespace ConsoleApplication1 { public struct Thing {
I have a container class that has a generic parameter which is constrained to
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace as2 { class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Caching; namespace ConsoleApplication4 { class
Namespace error cant find which one? using System; using System.Collections.Generic; using System.Linq; using System.Text;
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; namespace ConsoleApplication1 { public

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.