我有一个词典
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
如果我删除旧列表中的逐个项目我的新列表清除..
如何真正复制字典或列表数组?
由于
答案 0 :(得分:2)
您需要一个新列表,否则两个列表都是相同的,如果您从列表2中删除它,您也会从列表1中删除它,因为List(Of T)
是一个引用类型。您可以使用this list constructor:
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