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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:09:29+00:00 2026-05-23T06:09:29+00:00

How can I implement an easier solution, I’m looking for a better approach instead

  • 0

How can I implement an easier solution, I’m looking for a better approach instead having to create a complex implementation class, appreciate some advice.

Current I’m doing the following:

public interface IAddressCollection : IList<IAddress>
{
      new void Add(IAddress address);    
}

so then I’ve got to implement everything something like the below the code. I want to avoid doing this, I’m looking for a simple way.

public class AddressCollection : IAddressCollection
{
    private List<IAddress> _data = new List<IAddress>();

    new public void Add(IAddress applicationAddress)
    {
        // some logic here... e.g.
        if (!this.Any(x => x.AddressType != applicationAddress.AddressType))
        {
            _data.Add(applicationAddress);
        }
    }
    // Plus all the members of IList

EDIT******

How can I avoid implementing all IList members, and still have a base interface which implements a collection?

Furthermore how can u get 5 votes for “What’s the question exactly?” seriously, if he gave me a half decent answer I’d give him/she a vote but not for question to a question.

  • 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-23T06:09:30+00:00Added an answer on May 23, 2026 at 6:09 am

    The most common approach would be to derive from Collection<T>:

    public class AddressCollection : Collection<IAddress>, IAddressCollection
    {
        public AddressCollection() : base(new List<IAddress>())
        {
        }
    }
    

    Collection<T> implements IList<T>.

    UPDATE in response to comments.

    If you want to implement custom logic when adding or removing items, it’s better to override one or more of the protected virtual methods InsertItem, SetItem, RemoveItem and ClearItems, rather than implementing a new Add method.

    InsertItem is called whenever the public Add or Insert method is called to add an element to the list. Similarly SetItem is called when an element is replaced (myCollection[index] = newValue), RemoveItem is called when the public Remove method is called and ClearItems when the public Clear method is called.

    By doing this you will address the concern raised by Florian Greinacher in the comments – your custom logic will always be executed even if the list is used polymorphically as an IList<IAddress> or Collection<IAddress>

    For example, to match your sample, you could ignore inserting / setting items that don’t match a predicate with code something like the following (untested):

    protected override void InsertItem(int index, IAddress newItem)
    {
        if (!this.Any(x => x.AddressType != newItem.AddressType)
        {
            base.InsertItem(index, newItem);
        }
        else
        {
            // ... item does not match, do whatever is appropriate e.g. throw an exception
        }
    }
    
    protected override void SetItem(int index, IAddress newItem)
    {
        ... etc ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In java a class can implement Iterable which lets you use the foreach() statement
How can implement reordring in winforms datagridview by using the feature of drag and
Can someone please tell me how I can implement the following line of pseudo-code.
Anyone knows how i can implement re-arrangable divs (Drag the divs around on the
How I can implement dynamic Jtree, which shows created instances of clases? For example,
I know that classes can implement various special methods, such as __iter__ , __setitem__
How I can implement Background Processing in asp.net. Can anyone give a small and
I'm wondering how you can implement a program similar to tail -f in C/C++,
Any ideas of how best i can implement article revision history for a Java
I'd like to know if I can implement a method on MemoryCache that removes

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.