Remove elements from List(Of T)
I use the removeRange function to remove a couple elements f开发者_如何转开发rom a list, but the Count of the list seems to be intact, so I assume it didn't work the way I expected it too.... so, how do you remove elements? I mean, reduce the list's Count value ultimately.
RemoveRange
does indeed work. The first argument is the index at which you want to start removing, and the second is the number of elements to remove.
So:
Dim list = New List(Of Integer) From {1, 2, 3}
list.RemoveRange(0, 2)
Console.WriteLine(list.Count)
The above code will remove the elements 1
and 2
from the list and output "1" (the number of elements in the list after removal).
The List(Of T).RemoveRange
function does an in place removal so you should be seeing a modification to the Count
property. The only way you will not see a modification to the Count
entry or a thrown exception is if you pass 0 for the count parameter.
I think you what you are doing is
Suppose we have List
of Persons
class named as lstPersons
then
lstPersons.RemoveRange(..)
But it should be like
lstPersons = lstPersons.RemoveRange(..)
精彩评论