Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 897687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:55:22+00:00 2026-05-15T14:55:22+00:00

I know it must have been asked more than twice, but I could not

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:55:23+00:00Added an answer on May 15, 2026 at 2:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question have been asked endless times but I can't find a
I feel like this question must have been asked before but I must not
I know that this question must have been asked and answered a million times,
I know questions like this have been asked numerous times, but not quite this
I know this question has already been asked, but my question is more general
I know similar permutations of this question have been asked before, but the answers
I know this must be a n00b question, but I have to implement a
I know that similar questions have been asked all over the place, but I'm
Maybe it has been asked somewhere, but I am trying to find my question
I know I asked these question before but I did not get the right

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.