Freigeben über


(Hash)Set in the 3.5 BCL

Since it's coming up on 2 years since I complained that the BCL didn't have Set<T> I figured I might as well make sure people know about HashSet<T> in the upcoming Orcas BCL.

Now, I hate the name both because it collides with the same name in java.util (and hence will collide in MSDN searches), and also that it isn't the same Set<T> that PowerCollections (sponsored by the BCL team) went with, making a bumpier glide path for the class likely most used from PowerCollections, kind of missing the point of things being gradually "sucked back in" to the BCL as PowerCollections proves their usefulness (although I'm sure the BCL team would hate that phrasing of the situation).

However, at least now there's a data structure representing a set, so that's a good start :)

Union: 1, 2, 3, 4
Intersection: 2, 3

     class Program
    {
        public static string ToString<T>(IEnumerable<T> blah)
        {
            StringBuilder sb = new StringBuilder();
            foreach (T item in blah)
            {
                if (sb.Length != 0) { sb.Append(", "); }
                sb.Append(item.ToString());
            }
            return sb.ToString();
        }

        static void Main(string[] args)
        {
            HashSet<int> first = new HashSet<int>() { 1, 2, 3 };
            HashSet<int> second = new HashSet<int>() { 2, 3, 4 };
            Console.WriteLine("Union: {0}", ToString(first.Union(second)));
            Console.WriteLine("Intersection: {0}", ToString(first.Intersect(second)));
        }
    }