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

246
Views
Colección<T> RemoveAt vs RemoveItem

Método Collection.RemoveAt(Int32) frente a 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

¿Cuál es la diferencia entre estos dos métodos? Según los documentos, RemoveItem es virtual y RemoveAt no lo es, y esa es la única diferencia que puedo encontrar.

El código fuente extraído se parece a esto

 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

Entonces, mi pregunta más adecuada es ¿por qué hay dos métodos muy similares que difieren solo por ser virtuales o no? ¿Cuál es el caso de uso? ¿Es esto solo un cruft histórico? ¿Cuál es la historia?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

La documentación de RemoveAt incluye esta interesante nota al final de la página:

Las clases derivadas pueden invalidar RemoveItem(Int32) para cambiar el comportamiento de este método.

La conclusión final es que RemoveAt llama a RemoveItem y que las clases derivadas anulan el comportamiento anulando RemoveItem .

over 4 years ago · Santiago Trujillo Report

0

El motivo de tener dos métodos es permitir que el consumidor cree una subclase de Collection<T> que anulará el comportamiento de insertar/eliminar elementos. Puede anular InsertItem , RemoveItem , SetItem , ClearItems .

Por ejemplo, puede anular estos métodos en la subclase para enviar eventos (como ObservableCollection) o para registrar cualquier cambio en la colección.

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!