Saturn
Saturn

Reputation: 18149

Remove elements from List(Of T)

I use the removeRange function to remove a couple elements from 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.

Upvotes: 1

Views: 5623

Answers (3)

Harsh Baid
Harsh Baid

Reputation: 7249

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(..)

Upvotes: -1

JaredPar
JaredPar

Reputation: 754585

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.

Upvotes: 0

Dan Tao
Dan Tao

Reputation: 128317

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).

Upvotes: 4

Related Questions