Generic List's AddRange/InsertRange

Hello,

I have a class that inherits from List(Of <custom>); what I want to know, is in this class, I have:

public class MyClass
   Inherits List(Of MyObject)

   public shadows sub Add(item as MyObject)

   end sub

   public shadows sub Insert(index as Integer, item as MyObject)

   end sub

End Class

What I want to know is, in the AddRange/InsertRange implementation, will these methods call the base Add/Insert methods, or will it call the Add/Insert methods that I created above through shadowing, or doesn't it use these methods at all

Thanks.



Answer this question

Generic List's AddRange/InsertRange

  • Bearc0025

    The Add method will never be called, and Insert not neccessarily:

    If InsertRange gets an ICollection<T> (like List<T>) as the argument, it can use Array.Copy to copy the list items into its internal storage array. Only if the argument is not an ICollection<T>, the Insert method will be called for each item, which is probably a lot slower than copying between arrays.

    The AddRange methods calls InsertRange as well, so the same rules apply.


    PS: You can check out the source code for these methods yourself using Reflector, which is what I did.

  • Generic List's AddRange/InsertRange