C# HashSet Advantages

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

[ad_1]

A Set data structure was finally added to C# in the .NET Framework 3.5. It is a powerful data structure that makes life a little easier for programmers…

The C# Set is called a HashSet. It is modeled after a math set, meaning that elements must be unique. In other words a Set guarantees that there are no duplicate elements.

Now why create a Set data structure in C# when one could simply check a list before adding an element to see if it already exists? The answer: it is because searching a run-of-the-mill list is slow. A HashSet is fundamentally designed to allow fast searches, making inserts faster.

There are different implementations of Sets. Some make insertion and lookup operations super fast by hashing elements. However that means that the order in which the elements were added is lost. Other implementations preserve the added order at the cost of slower running times.

The HashSet class in C# goes for the second approach, thus preserving the order of elements. It is still much faster than a regular List. Some basic benchmarks showed that HashSet is decently faster when dealing with primary types (int, double, bool, etc.). It is a lot faster when working class objects. So that point is that HashSet is fast.

The only catch of HashSet is that there is no access by indices. To access elements you can either use an enumerator or use the built-in function to convert the HashSet into a List and iterate through that.

[ad_2]

Source by Armando Pensado

Related Posts

Pet Hamster Care - Are You Up to the Challenge?

Essential Eye Health Foods For Better Eyesight

[ad_1] In many ways the saying by Hippocrates is true today: "Let thy food be thy medicine and let thy medicine be thy food." This principle can be applied to…

Read more
Dwarf Hamster Care - A Pocket Pet For You?

What Are the Serving Sizes on Keurig Coffee Makers?

[ad_1] While different models of Keurig coffee makers will have different options for cup sizes, they all have one thing in common–the buttons you press to brew a cup of…

Read more
Dwarf Hamster Care - A Pocket Pet For You?

Singing Tips and Techniques – Drop Your Adam’s Apple and Extend Your High Singing Range

[ad_1] The “Adam’s Apple” is the container of the vocal cords. When we swallow it moves up to the top of the throat in order to help keep food or…

Read more
Dwarf Hamster Health and Breeding Tips

The Advantages and Disadvantages of Being Wealthy and Successful

[ad_1] As far as I know, almost everyone wants to become wealthy and successful. Being wealthy and successful allows you to lead a comfortable life, buy anything you want to…

Read more
Dwarf Hamster Health and Breeding Tips

9 Obstacles That Keep You From Achieving Your Goals

[ad_1] While you may have spent a lot of time working on your goals for the next year, there are plenty of things that are going to get in your…

Read more
Dwarf Hamster Care - A Pocket Pet For You?

Prostate Massage – How Much is Enough? How Much is Too Much?

[ad_1] Prostate massage is without a doubt one of the most powerful tools for healing a sick and painful prostate. It's not because of the massage itself. It is because…

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *

Thabet