What is the best way to clear an array of strings?

后端 未结 7 1377
被撕碎了的回忆
被撕碎了的回忆 2021-02-19 02:13

What is the best way to clear an array of strings?

7条回答
  •  悲哀的现实
    2021-02-19 02:58

    If you need to reinitialize with empty strings or other values not equal to Nothing/Null, you may get further using an extension method:

    Option Strict On : Option Explicit On : Option Infer On
    ...
    Public Delegate Sub ArrayForAllDelegate(Of T)(ByRef message As T)
    
    Public Function ForAll(Of T)(ByRef self As T(), f As ArrayForAllDelegate(Of T)) As T()
        Dim i = 0
        While i < self.Length
            f(self(i))
            i += 1
        End While
        Return self
    End Function
    

    Then your initialization code:

    Dim a = New String(3 - 1) {"a", "b", "c"}
    ...
    a.ForAll(Sub(ByRef el) el = "") 'reinitialize the array with actual empty strings
    

提交回复
热议问题