// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Collections.Generic; using System.Linq; public class HS { public static void Main(string[] args) { HashSet hs1 = new HashSet(); hs1.Add(1); hs1.Add(2); hs1.Add(3); HashSet hs2 = new HashSet(); hs2.Add(3); hs2.Add(4); foreach(int v in hs1) Console.Write(" " + v); Console.WriteLine(); foreach(int v in hs2) Console.Write(" " + v); Console.WriteLine(); // duplikater ignoreres hs1.Add(2); hs1.Add(3); foreach(int v in hs1) Console.Write(" " + v); Console.WriteLine(); // forenings mængde HashSet hs3 = new HashSet(hs1.Union(hs2)); foreach(int v in hs3) Console.Write(" " + v); Console.WriteLine(); // fælles mængde HashSet hs4 = new HashSet(hs1.Intersect(hs2)); foreach(int v in hs4) Console.Write(" " + v); Console.WriteLine(); // komplementær mængde HashSet hs5 = new HashSet(hs1.Except(hs2)); foreach(int v in hs5) Console.Write(" " + v); Console.WriteLine(); } }