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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:15:17+00:00 2026-06-15T02:15:17+00:00

I have the following classes: public class MyDocuments { public DateTime registeredDate; public string

  • 0

I have the following classes:

 public class MyDocuments
    {
        public DateTime registeredDate;
        public string version;
        public List<Document> registeredDocuments;
    }

    public class Document
    {
        public string name;
        public List<File> registeredFiles;
    }

    public class File
    {
        public string name;
        public string content;
    }

I have an instance of MyDocuments which has several documents in List<Document> registeredDocument. I get a new List<Document> from the user.

How can I verify that the new object doesn’t exist in the list? I want to compare by value not reference.

I’m thinking of using HashSet instead of List. Is this the proper approach?

  • 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-15T02:15:18+00:00Added an answer on June 15, 2026 at 2:15 am

    How are equality comparisons performed?

    Whenever the BCL classes want to perform an equality check between objects of some type T, they do so by calling one or both of the methods in some implementation of IEqualityComparer<T>. To get hold of such an implementation, the framework looks to EqualityComparer<T>.Default.

    As mentioned in the documentation, this property produces an IEqualityComparer<T> like this:

    The Default property checks whether type T implements the
    System.IEquatable<T> interface and, if so, returns an
    EqualityComparer<T> that uses that implementation. Otherwise, it
    returns an EqualityComparer<T> that uses the overrides of
    Object.Equals and Object.GetHashCode provided by T.

    What are my options?

    So, in general, to dictate how equality comparisons should be performed you can:

    1. Explicitly provide an implementation of IEqualityComparer<T> to the class or method that performs equality checks. This option is not very visible with List<T>, but many LINQ methods (such as Contains) do support it.
    2. Make your class implement IEquatable<T>. This will make EqualityComparer<T>.Default use this implementation, and is a good choice whenever there is an obvious “natural” way to compare objects of type T.
    3. Override object.GetHashCode and object.Equals without implementing IEqualityComparer<T>. However, this is simply an inferior version of #2 and AFAIK should always be avoided.

    Which option to pick?

    A good rule of thumb is: if there is an obvious and natural way to compare objects of class T, consider having it implement IEquatable<T>; this will make sure your comparison logic is used throughout the framework without any additional involvement. If there is no obvious candidate, or if you want to compare in a manner different than the default, implement your own IEqualityComparer<T> and pass the implementation as an argument to the class or method that needs to perform equality checks.

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

Sidebar

Related Questions

I have the following two classes: public class Address { public string AddressLine1 {
I have the following classes [XmlRoot] public class AList { public List<B> ListOfBs {get;
I have 3 following classes: public class BaseProperty1{ public string Property1 {get; set;} }
I have the following classes: public class Person { public String FirstName { set;
I have the following classes: public class MyItems : List<MyItem> { ... } public
I have the following classes: public class Group { private long id; private String
Say I have the following classes: public class A { public string x; }
I have the following classes: public class DrawableComplexEntity2D { public List<GameComponent> Components { get;
Say I have the following classes public class TestA { public string Blah {
Let's say I have the following classes : public class MyProductCode { private String

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.