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