在vb.net中复制List(Of T)类的最快方法?

时间:2014-12-03 18:40:48

标签: vb.net linq class

VB.NET 2012 我创建了一个foo的部分列表,它将在构建之后保持静态,然后是第二个Foo列表,它将始终包含部分列表中的所有内容。我的问题是......将一个列表复制到另一个列表的快速或最快方法是什么?有没有更快的方式,没有循环?

请参阅Combine

中的方法Class1
Option Explicit On
Option Strict On
Public Class Form1
    Private _initFoo As New Class1()
    Private _postFoo As New Class1()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        _initFoo.Init()
        _postFoo.Combine(_initFoo, _postFoo) ' **
    End Sub
End Class


Option Explicit On
Option Strict On
Public Class Class1
    Private FooLst As New List(Of Foo)

    Public Class Foo
        Public Property Item1 As String
        Public Property Item2 As String
    End Class

    Public Sub Init()
        FooLst.Add(New Class1.Foo With {.Item1 = "1", .Item2 = "A"})
        FooLst.Add(New Class1.Foo With {.Item1 = "2", .Item2 = "B"})
    End Sub

    Public Sub Combine(readFrom As Class1, writeTo As Class1) ' **
        ' Is there a faster or way to copy one list to the other?
        ' possibly without looping though each item in the readFrom list?
        For Each f As Foo In readFrom.FooLst
            writeTo.FooLst.Add(New Foo With {.Item1 = f.Item1, .Item2 = f.Item2})
        Next
    End Sub
End Class

1 个答案:

答案 0 :(得分:5)

您可以使用AddRange将一个列表的内容添加到现有列表中:

list1.AddRange(list2)

这会将list2的内容添加到list1,并保留list1中的原始项目。