将集合转换为数组的最优化/有效的方法?
我知道这个问题肯定被问过两次以上,但我找不到任何适用的问题来回答这个问题。
我有一个集合,我希望从 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不需要使用
CreateInstance
来创建数组,你知道类型,甚至可以将其作为特定类型的数组返回:这当然取决于效率如何您的
ICollection(Of T).Count
和ICollection(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:How efficient this is, of course relies on how efficient your implementations of
ICollection(Of T).Count
andICollection(Of T).CopyTo
are.您应该更改您的AddRange
方法以采用IEnumerable
或ICollection
(如果您需要计数
)。这样,您根本不需要调用
ToArray
,从而节省了内存分配。您的实现看起来不错。
但请注意,
Dictionary
是无序的。You should change yourAddRange
method to take anIEnumerable<T>
or anICollection<T>
(if you needCount
).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.