Copy the elements of a HashSet to an array in CSharp

Description

The following code shows how to copy the elements of a HashSet to an array.

Example


/*from   w  w  w  .  j  a v  a2 s  .  c o m*/
using System;

using System.Collections.Generic;


public class MainClass
{
    public static void Main(String[] argv)
    {
        HashSet<int> evenNumbers = new HashSet<int>();
        for (int i = 0; i < 5; i++)
        {
            evenNumbers.Add(i * 2);
        }
        int[] intArray = new int[5];
        evenNumbers.CopyTo(intArray);
        
        foreach(int i in intArray){
           Console.WriteLine(i);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary