将集合转换为数组的最优化/有效的方法?

发布于 2024-09-08 12:43:46 字数 1508 浏览 3 评论 0原文

我知道这个问题肯定被问过两次以上,但我找不到任何适用的问题来回答这个问题。

我有一个集合,我希望从 .NET 2.0 中检索一个数组。换句话说,我想将集合转换为数组。到目前为止,我有以下内容:

Public Class Set(Of T)
    Implements IEnumerable(Of T)
    Implements ICollection(Of T)
    Implements IEqualityComparer(Of T)

    Private _set as Dictionary(Of T, Object)

    // Implementing the interfaces here...

    Public Function ToArray() As Array
        Dim arr As Array = Array.CreateInstance(GetType(T), Me.Count)
        Me.CopyTo(arr, 0)
        Return arr
    End Function
End Class

然后,当我调用它时,我只需:

Dim propertiesToLoad As CustomSet(Of String) = New CustomSet(Of String)()
// Initializing my CustomSet here...

Dim searcher As DirectorySearcher = New DirectorySearcher()
Dim entry As DirectoryEntry = New DirectoryEntry("LDAP://" & Environment.UserDomain)

searcher.SearchRoot = entry
searcher.SearchScope = SearchScope.Subtree
searcher.Filter = someFilter
searcher.PropertiesToLoad.AddRange(propertiesToLoad.ToArray())

// Launching search here...

在 .NET 2.0 中是否有更有效的方法来做到这一点?

编辑#1< /strong>

我的 CustomSet(Of T) 中 Count 和 CopyTo 的实现:

Public ReadOnly Property Count As Integer Implements ICollection(Of T).Count
    Get
        Return _set.Keys.Count
    End Get
End Property

Public Sub CopyTo(ByVal array As T, ByVal arrayIndex As Integer) Implements ICollection(Of T).CopyTo
    _set.Keys.CopyTo(array, arrayIndex)
End Sub

I know it must have been asked more than twice, but I could not find any applicable question to answer the matter.

I have a collection which I wish to retrieve an array from in .NET 2.0. In other words, I want to convert a collection into an array. So far I have the following:

Public Class Set(Of T)
    Implements IEnumerable(Of T)
    Implements ICollection(Of T)
    Implements IEqualityComparer(Of T)

    Private _set as Dictionary(Of T, Object)

    // Implementing the interfaces here...

    Public Function ToArray() As Array
        Dim arr As Array = Array.CreateInstance(GetType(T), Me.Count)
        Me.CopyTo(arr, 0)
        Return arr
    End Function
End Class

And then, when I'm calling it, I simply have to:

Dim propertiesToLoad As CustomSet(Of String) = New CustomSet(Of String)()
// Initializing my CustomSet here...

Dim searcher As DirectorySearcher = New DirectorySearcher()
Dim entry As DirectoryEntry = New DirectoryEntry("LDAP://" & Environment.UserDomain)

searcher.SearchRoot = entry
searcher.SearchScope = SearchScope.Subtree
searcher.Filter = someFilter
searcher.PropertiesToLoad.AddRange(propertiesToLoad.ToArray())

// Launching search here...

Is there a more effective way to do it in .NET 2.0?

EDIT #1

Implementations of Count and CopyTo in my CustomSet(Of T):

Public ReadOnly Property Count As Integer Implements ICollection(Of T).Count
    Get
        Return _set.Keys.Count
    End Get
End Property

Public Sub CopyTo(ByVal array As T, ByVal arrayIndex As Integer) Implements ICollection(Of T).CopyTo
    _set.Keys.CopyTo(array, arrayIndex)
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疾风者 2024-09-15 12:43:46

你不需要使用CreateInstance来创建数组,你知道类型,甚至可以将其作为特定类型的数组返回:

Public Function ToArray() As T()
  Dim arr(Me.Count) As T
  Me.CopyTo(arr, 0)
  Return arr
End Function

这当然取决于效率如何您的 ICollection(Of T).CountICollection(Of T).CopyTo 的实现是。

You don't need to use CreateInstance to create the array, you know the type, and you can even return it as an array of the specific type:

Public Function ToArray() As T()
  Dim arr(Me.Count) As T
  Me.CopyTo(arr, 0)
  Return arr
End Function

How efficient this is, of course relies on how efficient your implementations of ICollection(Of T).Count and ICollection(Of T).CopyTo are.

生生漫 2024-09-15 12:43:46

您应该更改您的 AddRange 方法以采用 IEnumerableICollection(如果您需要 计数)。
这样,您根本不需要调用 ToArray,从而节省了内存分配。

您的实现看起来不错。
但请注意,Dictionary 是无序的。

You should change your AddRange method to take an IEnumerable<T> or an ICollection<T> (if you need Count).
This way, you won't need to call ToArray at all, saving a memory allocation.

Your implementation looks fine.
However, note that Dictionary<TKey, TValue> is unordered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文