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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:57:48+00:00 2026-06-07T02:57:48+00:00

Let’s say I have a class like this : public class MyClass { public

  • 0

Let’s say I have a class like this :

public class MyClass {

    public int Id { get; set;}
    public string Name { get; set; }

}

Please note that this class, in my case, is out of control (from a 3rd party library) and does not implement equality comparers.

Now I want to have a collection of this items that allow me to remove items based on their index.

I’m expecting to be able to do :

void Foo()
{
    var collection = new MyClassCollection();

    var a = new MyClass { Id = 1, Name = "A" };
    var b = new MyClass { Id = 2, Name = "B" };

    collection.Add(a);
    collection.Add(b);

    // Other instance, that can come from external code
    var otherA = new MyClass { Id = 1, Name = "A" };
    var otherB = new MyClass { Id = 2, Name = "B" };

    collection.Remove(otherA);

    Console.WriteLine(collection.Count); // should output 1

    Console.WriteLine(collection.Contains(otherB)); // should be true

    collection.Add(otherB); // 

    Console.WriteLine(collection.Count); // should still output 1
}

In fact, only the ID should be considered when searching within the collection. This code is part of a project that rely on serialized DTO, and thus, cannot rely on instances.

How should I implement MyClassCollection?

I think about two possibilities, but neither looks like elegant for me :

  1. inherits from List<MyClass> and add new methods. This will leave the .Add and .Remove as is and can cause troubles for consumer code
  2. Create a class that implements ICollection<MyClass>. Create an internal Dictionary<int, MyClass> to store the values, and use the dictionary builtin features to check keys before adding or removing. Implement all methods of the interface by wrapping the internal dictionary values

I’d appreciate your point of view regarding my case.

PS: I expect to have very few elements in the list (<100 items). Performance won’t be an issue

Edit: I’m using .Net 3.5 SP1.

Edit2: according Jon advise, here is my implementation :

public class MyClassCollection : HashSet<MyClass>
{
    private class MyClassIDComparer : IEqualityComparer<MyClass>{

        public bool Equals(MyClass x, MyClass y)
        {
            if (x == null && y == null) return true;
            else if (x == null || y == null) return false;
            else return x.ID == y.ID;
        }

        public int GetHashCode(MyClass obj)
        {
            return obj.ID.GetHashCode();
        }
    }
    public MyClassCollection() : base(new MyClassIDComparer())
    {

    }
}
  • 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-07T02:57:50+00:00Added an answer on June 7, 2026 at 2:57 am

    HashSet<MyClass> is a good fit for your usage case because it models your intent and can be configured with a custom IEqualityComparer<MyClass> (thus it doesn’t matter that MyClass does not implement IEquatable<MyClass>).

    Unfortunately HashSet does not implement IList, so it cannot directly do index-based access — but do you really need that? Perhaps this requirement can be relaxed by rethinking some part of your business logic?

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

Sidebar

Related Questions

Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have the following classes : public class MyProductCode { private String
Let's say that I have a set of relations that looks like this: relations
Let's say we have: @Entity public class Order { @Id private int id; @OneToMany(mappedBy=order)
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I can call a method like this: core::get() . What is the
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say I have table with column 'URL' whrere I store urls like this

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.