How to convert a HashSet to an array in C#

2 Answers

0 votes
using System;
using System.Collections.Generic;
 
class Program
{
    static void Main() {
        HashSet<string> hset = new HashSet<string> {"c#", "vb.net", "c", "c++", "python"};
         
        string[] array = new string[hset.Count];
 
        hset.CopyTo(array);
          
        Console.WriteLine(string.Join(", ", array));
    }
}
 
 
 
 
/*
run:
 
c#, vb.net, c, c++, python
 
*/

 



answered Oct 28, 2019 by avibootz
edited Aug 7, 2022 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main() {
        HashSet<string> hset = new HashSet<string> {"c#", "vb.net", "c", "c++", "python"};
        
        string[] array = hset.ToArray();

        hset.CopyTo(array);
         
        Console.WriteLine(string.Join(", ", array));
    }
}




/*
run:

c#, vb.net, c, c++, python

*/

 



answered Oct 28, 2019 by avibootz
edited Aug 7, 2022 by avibootz

Related questions

1 answer 266 views
1 answer 255 views
255 views asked Oct 29, 2019 by avibootz
2 answers 296 views
1 answer 208 views
208 views asked Jul 18, 2022 by avibootz
1 answer 219 views
219 views asked Feb 7, 2024 by avibootz
1 answer 218 views
218 views asked Mar 9, 2023 by avibootz
...