Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
Collection<T> RemoveAt vs RemoveItem

Collection.RemoveAt(Int32) Method vs Collection.RemoveItem(Int32)

https://docs.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.collection-1.removeat?view=net-6.0

https://docs.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.collection-1.removeitem?view=net-6.0

What is the difference between these two methods? According to the docs RemoveItem is virtual and RemoveAt is not, and that is the only difference I can find.

The excerpted source code looks something like this

    IList<T> items;

    ....

    public void RemoveAt(int index) {
        if( items.IsReadOnly) {
            ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
        }

        if (index < 0 || index >= items.Count) {
            ThrowHelper.ThrowArgumentOutOfRangeException();
        }

        RemoveItem(index);
    }

    ...

    protected virtual void RemoveItem(int index) {
        items.RemoveAt(index);
    }

https://github.com/microsoft/referencesource/blob/master/mscorlib/system/collections/objectmodel/collection.cs

So my question more properly is why are there two very similar methods differing only by being virtual or not? What is the use case? Is this just historical cruft? What's the story?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The documentation for RemoveAt includes this interesting note at the bottom of the page:

Derived classes can override RemoveItem(Int32) to change the behavior of this method.

The ultimate conclusion is that RemoveAt calls RemoveItem, and that derived classes override the behavior by overriding RemoveItem.

over 4 years ago · Santiago Trujillo Report

0

Reason for having two methods is to allow consumer to create subclass of Collection<T> that will override behavior of inserting\removing items. You can override InsertItem, RemoveItem, SetItem, ClearItems.

For example, you can override this methods in subclass to send events (like ObservableCollection) or to log any collection change.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!