If you .Remove () a specific element from the <T> list, does it also remove the "empty slot"?
Feel free to correct my terminology and understanding of the list if necessary.
If I have a list of five elements, where each element has a unique identifier ...
[item 1] [item 2] [item 3] [item 4] [item 5] and I delete the item with id 72 (for example) and it happens to be the third in the list ...
will it be so ...
[item 1] [item 2] [null] [item 4] [item 5] or how is it
[item 1] [item 2] [item 3] [item 4] where what used to be point 4 is now point 3, etc.
Please explain if you can :)
In your example
[item 1] [item 2] [item 3] [item 4] [item 5] if you delete [item 3], then the List-class will simply copy the part of the array "behind" this element into the index of the elements, the internal list array will look like this:
[item 1] [item 2] [item 4] [item 5] [default(item)] Now you canβt access the last item, since List saves a size variable to keep track of how many items you can access. So practically, List looks like your second example from the outside, but the internal array can really be bigger.
Edit: Also note that when reused, the array is much larger, since List <> will change its size when inserting elements (it will double the size of the array when there is not enough space for a new element). For example, if you add 5 items to the list, your list will look like this:
[item 1] [item 2] [item 3] [item 4] [item 5] [default(item)] [default(item)] [default(item)] The second option. It behaves like a List , not like an array.
You can delete an item with a specific index. The indices of the previous elements with this and higher indices are reduced by 1.
Actually, with the implementation of System.Collections.Generic.List<T> base array that is used to store the elements will be like this:
[item 1] [item 2] [item 4] [item 5] [null] That is, without changing the size of the array, the 3rd element was deleted, and the 4th and 5th positions were shifted.
Of course, if you list a collection, trailing zeros will be omitted since the size of the collection is used to determine where to stop. (Edit: And attempts to access characters beyond the logical end of the collection will fail.)
My statement was corrected using technical observations, but for enumerated purposes, the List object, when you delete the element that it is gone, as your second example.