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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:15:31+00:00 2026-05-16T15:15:31+00:00

I have a strongly typed list of custom objects, MyObject , which has a

  • 0

I have a strongly typed list of custom objects, MyObject, which has a property Id, along with some other properties.

Let’s say that the Id of a MyObject defines it as unique and I want to check if my collection doesn’t already have a MyObject object that has an Id of 1 before I add my new MyObject to the collection.

I want to use if(!List<MyObject>.Contains(myObj)), but how do I enforce the fact that only one or two properties of MyObject define it as unique?

I can use IComparable? Or do I only have to override an Equals method? If so, I’d need to inherit something first, is that right?

  • 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-16T15:15:31+00:00Added an answer on May 16, 2026 at 3:15 pm

    List<T>.Contains uses EqualityComparer<T>.Default, which in turn uses IEquatable<T> if the type implements it, or object.Equals otherwise.

    You could just implement IEquatable<T> but it’s a good idea to override object.Equals if you do so, and a very good idea to override GetHashCode() if you do that:

    public class SomeIDdClass : IEquatable<SomeIDdClass>
    {
        private readonly int _id;
        public SomeIDdClass(int id)
        {
            _id = id;
        }
        public int Id
        {
            get { return _id; }
        }
        public bool Equals(SomeIDdClass other)
        {
            return null != other && _id == other._id;
        }
        public override bool Equals(object obj)
        {
            return Equals(obj as SomeIDdClass);
        }
        public override int GetHashCode()
        {
            return _id;
        }
    }
    

    Note that the hash code relates to the criteria for equality. This is vital.

    This also makes it applicable for any other case where equality, as defined by having the same ID, is useful. If you have a one-of requirement to check if a list has such an object, then I’d probably suggest just doing:

    return someList.Any(item => item.Id == cmpItem.Id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a company object which has a list of branch objects, my company
As the questions says I have always believed that a strongly typed list is
I have a strongly-typed MVC View Control which is responsible for the UI where
I have a strongly typed view of type ProductListingViewModel which in turn contains a
I Have a Strongly typed user control which i use for Searching a certain
Ok I have a View thats is strongly typed as a collection of MyObject
I have a strongly typed user control (partial) and I'd like to be able
There's no strongly typed View() method to return an ActionResult. So, suppose I have
We have our own ORM we use here, and provide strongly typed wrappers for
I have a partial view that renders a list of objects into a table

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.