【发布时间】:2014-09-21 21:55:48
【问题描述】:
我想知道在 C# 或 VB.NET 中如何使用 ArrayList 对象的 CopyTo 方法将其内容复制到其他 ArrayList (不是一个简单的数组)。
注意:我不是在寻找像 ArrayList 的 Clone 方法或其他建议之类的东西,我需要使用 CopyTo 方法来知道我是否应该丢弃问题在其他情况下。
这是 VB.NET 中的代码示例:
Dim ArrayList1 as New ArrayList
Dim ArrayList2 as New ArrayList
ArrayList1.Add({"test-Item1-1", "test-Item1-2", "Test-Item1-3"})
' This says that the matrix of the destiny Array is too short.
ArrayList1.CopyTo(ArrayList2.ToArray)
' This shows the typical CastIterator exception 'cause LINQ.
ArrayList1.CopyTo(ArrayList2.Cast(Of Array))
' This says that the ArrayList can't be converted to an Array.
ArrayList1.CopyTo(CType(ArrayList2, Array))
【问题讨论】:
-
旁注:为什么不使用
List<String>来代替? -
@Tim Schmelter '因为用于 MY.Settings,不接受通用集合 :(。感谢您的评论
-
@Tim Schmelter 是的,但是 stringcollection 需要 String 而不是 String(),ArrayList 应该更容易使用,而不是使用分隔符来拆分普通 String,我真的对替代品不感兴趣,我知道替代方案,我只是想知道“CopyTo”方法的正确用法来丢弃问题,谢谢您的评论。
标签: c# .net arrays vb.net arraylist