Feel free to correct my terminology, and understanding of the List if needed..
If I have a list of five items where each item has a unique ID...
[item 1]
[item 2]
[item 3]
[item 4]
[item 5]
and I remove item with ID 72 (for example) and it happens to be 3rd in the List...
will it end up like this...
[item 1]
[item 2]
[null]
[item 4]
[item 5]
or like this
[item 1]
[item 2]
[item 3]
[item 4]
where what used to be item 4 is now item 3, etc.
Please explain if you can :)
RemoveAt (Int32) Method is used to remove the element at the specified index of the List<T>. Properties of List: It is different from the arrays.
An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.
pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.
Removes the first occurrence of a specific object from the List<T>.
Second option. It behaves as a List, not as an array.
It is possible to remove an element at a particular index. The indices of previous elements at that and higher indices are decreased by 1.
Actually, with the implementation of System.Collections.Generic.List<T>
, the underlying array that is used to store items will end up like this:
[item 1]
[item 2]
[item 4]
[item 5]
[null]
That is, without changing the size of the array, the 3rd item has been removed and the 4th and 5th items have been shifted.
Of course if you enumerate the collection the trailing nulls are omitted because the size of the collection is used to determine where to stop. (Edit: And attempts to access indicies beyond the logical end of the collection will fail.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With