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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:14:03+00:00 2026-06-09T13:14:03+00:00

I’ve spotted some really nasty code in one class (let’s call it MyClass )

  • 0

I’ve spotted some really nasty code in one class (let’s call it MyClass) that uses List of T.Add/Remove methods extensively. Also the same class exposes that collection as property with IList of T type, so changing the type would involve some refactoring.

Also, that MyClass collection is used as a container for event listener objects, so client code just subscribes (adds itself to collection) and unsubscribes (removes itself from collection). Order doesn’t matter, whether it’s start/middle/end of the list.

In order to improve performance, I would like to replace internal implementation detail, replace List of T with…. I don’t know what. I tried LinkedList of T, but it does not implement IList of T. I looked at IList of T at MSDN, but there’s no list of classes that implement IList, so I could compare documentations.

Another thing is that I tried to change IList of T to ICollection of T, and that could be a solution (as client code of MyClass uses Add/Remove methods, so no refactoring is necessary) but interesting thing happened:

        LinkedList<string> list = new LinkedList<string>();
        list.Add("test");

This code does not compile as I get an error:

‘System.Collections.Generic.LinkedList’ does not contain a
definition for ‘Add’ and no extension method ‘Add’

But when I changed it to:

        ICollection<string> list = new LinkedList<string>();
        list.Add("test");

then I worked. Could you explain why first sample didn’t compile + tell what is the fastest implementation of IList of T in .NET Framework in terms of Adding/Removing items?

Thanks.

  • 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-09T13:14:05+00:00Added an answer on June 9, 2026 at 1:14 pm

    There is no one single class that is fastest at adding and removing. They are all built to be faster in certain circumstances, and they all have their drawbacks as well. List can quickly add to the end, most of the time, but it will periodically have an expensive add that involves copying every element. As you start adding items closer to the start it gets more and more expensive. Removing is cheap at the end, and more and more expensive as you move towards the start.

    LinkedList can efficiently add to the start or end, but those adds are still quite a bit slower than adding to the end of a List (but without any really large adds). Adding to positions other than the start or end of a LinkedList get expensive quick. In most practical situations, LinkedList isn’t a good option from the perspective of performance.

    I could go on, but it would be a while. The point remains that we need to know more details about how the collection is being used in order to suggest a more performant alternative (or a different way of using the existing collection).

    Since you have described that what you have is just a set of data that doesn’t need to be ordered and just need to efficiently add/remove/iterate all the best collection would be a HashSet<T>. You can do some research yourself on the inner workings of a hash based data structure (it’s fairly cool) but the point is that it is an unordered data structure with extremely efficient adding/removing of elements, and that you can efficiently iterate. This is ideal for your circumstances. The one thing to keep in mind with a hash based data structure is that your objects need to have implementations of GetHashCode and Equals that are meaningful (the default ones usually aren’t for your custom types). If you’re using something like int or string as the key it will already have good implementations.

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.