C#’s HashSets

One thing that you encounter whenever you interview for a position as a .NET developer the technical question. That can make many forms, such as the famous “fizz-buzz” puzzle. But one that has always stumped me is how to find common elements between two arrays of the same data type. I’ve always come up with using two loops, one within another, which is of type O(n2). In the .NET framework I’ve recently learned of HashSets, which are extremely cool. This makes finding common elements easy. To learn more about them I decided to write a simple console app to try it out. This is based upon the MSDN online article on HashSets, which I’ve expanded upon. Here’s the source code:

using System;
using System.Collections.Generic;
using System.Linq;
namespace SimpleHashSet
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Learning about HashSets\r\n\r\n");
HashSet<int> setA = new HashSet<int>();
HashSet<int> setB = new HashSet<int>();
for (int i = 0; i < 5; i++)
{
setA.Add(i * 2);
setB.Add(i * 2 + 1);
}
//now, just add 100 to both
setA.Add(100);
setB.Add(100);
Console.WriteLine("setA contains {0} elements:", setA.Count);
DisplaySet(setA);
Console.WriteLine("setB containts {0} elements:", setB.Count);
DisplaySet(setB);
Console.WriteLine();
//what is the intersection?
HashSet<int> theIntersection = new HashSet<int>(setA);
theIntersection.Intersect(setB);
Console.WriteLine("Using the Intersect method on setA with setB");
DisplaySet(theIntersection);
Console.WriteLine();
//what is the intersecion with about?
HashSet<int> intersectWith = new HashSet<int>(setA);
intersectWith.IntersectWith(setB);
Console.WriteLine("Using the IntersectWith method on setA with setB");
DisplaySet(intersectWith);
Console.WriteLine();
//what about the union method?
HashSet<int> theUnion = new HashSet<int>(setA);
theUnion.Union(setB);
Console.WriteLine("Using the Union method on setA with setB");
DisplaySet(theUnion);
Console.WriteLine();
//what about the UnionWith method?
HashSet<int> unionWith = new HashSet<int>(setA);
unionWith.UnionWith(setB);
Console.WriteLine("Using the UnionWith method on setA with setB");
DisplaySet(unionWith);
Console.WriteLine();
//what is the Except method all about?
HashSet<int> theException = new HashSet<int>(setA);
theException.Except(setB);
Console.WriteLine("Using the Except method on setA with setB");
DisplaySet(theException);
Console.WriteLine();
//now try the ExceptWith method
HashSet<int> exceptWith = new HashSet<int>(setA);
exceptWith.ExceptWith(setB);
Console.WriteLine("Using the ExceptWith method on setA with setB");
DisplaySet(exceptWith);
//this is the end
Console.WriteLine("\r\n\r\nPress any key to quit.");
Console.ReadKey();
}
private static void DisplaySet(HashSet<int> passedSet)
{
Console.Write("{");
foreach (var item in passedSet)
{
Console.Write(" {0}", item);
}
Console.WriteLine(" }");
}
}
}
If you run the preceding code you’ll get this in the command window:
2016-03-11
Don’t know about you, but I find things like this handy to use a refer back to.

An excellent compilation of what’s new in XAML and Windows 10

I’ve behind in listening to the Dot Net Rocks podcast. (Sorry Carl and Richard.) I was listening to one from May of this year. They mentioned a web page by Sahil Malek titled New in XAML VS2015 Windows 10. Sahil has made several tweets about what’s new in XAML in Windows 10. I’m very thankful that he’s tweeted on all of these cool, new things!!