O'Reilly logo

Everyday Data Structures by William Smith

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Initializing sets

Sets are not terribly commonplace in development, but each of the languages we are examining supports data structures with some form of concrete implementations. Here are some examples of initializing a set, adding a few values to the collection including one duplicate, and printing the set's count to the console after each step.

C#

C# provides a concrete implementation of the set data structure through the HashSet<T> class. Since this class is generic, the caller may define the type used for elements. For example, the following example initializes a new set where the elements will be string types:

 HashSet<string, int> mySet = new HashSet<string>(); mySet.Add("green"); Console.WriteLine("{0}", mySet.Count); mySet.Add("yellow"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required