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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:20:31+00:00 2026-06-08T04:20:31+00:00

I’m trying to perform a good comparison when I use List.Contains(T item) . The

  • 0

I’m trying to perform a good comparison when I use List.Contains(T item).

The problem is that I’m using BaseItem as a list item. And I need to verify if one object inside of the list has the same properties values that the object which I plan to add.

For example:

public abstract class BaseItem
{
    // some properties

    public override bool Equals(object obj)
    {
        return obj != null && this.GetType() == obj.GetType();
    }
}

public class ItemA : BaseItem
{
    public int PropertyA { get; set; }

    public override bool Equals(object obj)
    {
        if (base.Equals(obj) == false)
            return false;

        return (this.PropertyA == (obj as ItemA).PropertyA;
    }
}

public class ItemB : BaseItem
{
    public int PropertyB { get; set; }

    public override bool Equals(object obj)
    {
        if (base.Equals(obj) == false)
            return false;

        return this.PropertyB == (obj as ItemB).PropertyB;
    }
}

public class Program
{
    static void Main(string[] args)
    {
        List<BaseItem> items = new List<BaseItem>()
        {
            new ItemB() { PropertyB = 3 },
            new ItemA() { PropertyA = 2 },
            new ItemB() { PropertyB = 2 }
        };

        BaseItem newItem = new ItemA() { PropertyA = 2 };
        items.Contains(newItem);  // should return 'True', because the first element is equals than 'newItem'
    }
}

I’m not sure if it’s correct to override Equals method or should I have to implement IEquality interface.

  • 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-08T04:20:33+00:00Added an answer on June 8, 2026 at 4:20 am

    The standard way to test for collection membership (list membership, hashset membership, …) is to use the .Contains method, which in turn uses IEquatable<T>.Equals. The default implementation of IEquatable<T>.Equals (if you do not provide your own implementation) uses Object.Equals(Object)).

    You can certainly implement IEquatable<T>. This will improve performance of the Contains check slightly. See msdn.microsoft.com/en-us/library/ms131190.aspx.

    If you implement IEquatable<T> you should still override Object.Equals and Object.GetHashCode to provide consistent behavior

    If you implement Equals, you should also override the base class implementations of Object.Equals(Object) and GetHashCode so that their behavior is consistent with that of the IEquatable.Equals method.

    http://msdn.microsoft.com/en-us/library/ms131190.aspx

    See this question for a related discussion

    Simplify Overriding Equals(), GetHashCode() in C# for Better Maintainability

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.