.net real如何复制字典或列表数组?

.net real如何复制字典或列表数组?,.net,vb.net,list,dictionary,.net,Vb.net,List,Dictionary,我有一本字典 Dim List4x As Dictionary(Of Byte, List(Of Byte)) = DuplicateDic(ByteList4) Public Shared Function DuplicateDic(ByVal List As Dictionary(Of Byte, List(Of Byte))) As Dictionary(Of Byte, List(Of Byte)) Dim kv As New Dictionary(Of Byte, List(

我有一本字典

Dim List4x As Dictionary(Of Byte, List(Of Byte)) = DuplicateDic(ByteList4)

Public Shared Function DuplicateDic(ByVal List As Dictionary(Of Byte, List(Of Byte))) As Dictionary(Of Byte, List(Of Byte))
    Dim kv As New Dictionary(Of Byte, List(Of Byte))
    For Each itm As KeyValuePair(Of Byte, List(Of Byte)) In List
        kv.Add(itm.Key, itm.Value)
    Next
    Return kv
End Function
如果我逐个删除旧列表中的项目,我的新列表将被清除

real如何复制字典或列表数组


谢谢

您需要一个新列表,否则两个列表是相同的,如果您从列表2中删除它,您也将从列表1中删除它,因为
list(Of T)
是一个引用类型。您可以使用:

公共共享函数DublicateList(ByVal列表作为字典(字节的,列表(字节的)))作为字典(字节的,列表(字节的))
Dim kv作为新字典(字节,列表(字节))
对于列表中的每个itm作为KeyValuePair(字节的列表,字节的列表
Dim newList As New List(Of Byte)(itm.Value)'A
List(Of T)
是一种引用类型(A类)。您必须为新词典创建一个全新的列表。您可以像使用字典一样迭代每个列表,也可以创建一个列表。
Public Shared Function DublicateList(ByVal List As Dictionary(Of Byte, List(Of Byte))) As Dictionary(Of Byte, List(Of Byte))
    Dim kv As New Dictionary(Of Byte, List(Of Byte))
    For Each itm As KeyValuePair(Of Byte, List(Of Byte)) In List
        Dim newList As New List(Of Byte)(itm.Value)  ' <----- HERE !!!
        kv.Add(itm.Key, newList)
    Next
    Return kv
End Function