How can I delete an item from an array in VB.NET?

前端 未结 11 1702
别那么骄傲
别那么骄傲 2020-12-05 18:14

How can I delete an item from an array in VB.NET?

11条回答
  •  执笔经年
    2020-12-05 18:34

    My favorite way:

    Imports System.Runtime.CompilerServices
    
     _
    Public Sub RemoveAll(Of T)(ByRef arr As T(), matching As Predicate(Of T))
        If Not IsNothing(arr) Then
            If arr.Count > 0 Then
                Dim ls As List(Of T) = arr.ToList
                ls.RemoveAll(matching)
                arr = ls.ToArray
            End If
        End If
    End Sub
    

    Then in the code, whenever I need to remove something from an array I can do it by some property in some object in that array having a certain value, like:

    arr.RemoveAll(Function(c) c.MasterContactID.Equals(customer.MasterContactID))
    

    Or if I already know the exact object I want to remove, I can just do:

    arr.RemoveAll(function(c) c.equals(customer))
    

提交回复
热议问题