Alex Rivera | Logout

Retrieve an object from hashset in C#

Asked 2011-01-06T10:18:28.453
15

Possible Duplicate:
Why can't I retrieve an item from a HashSet without enumeration?

I need to add a lot of objects to a Set.and I should retrieve them very fast. The only way that I know is using hash. But the HashSet class in C# doesn't contain any 'Get' method. The dictionary class isn't useful because Finding an object is very time-consuming in a dictionary.

Edit
Report

1 Answer

1

The HashSet represents a number of values. You can look if a certain value is in the set by using Contains(). A Dictionary is for storing objects and retrieving it by its hash (key).

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        public enum EnumA
        {
            One,
            Two,
            Three,
            Four
        }

        static void Main(string[] args)
        {
            HashSet<EnumA> test = new HashSet<EnumA>();

            test.Add(EnumA.Four);

            Console.WriteLine("Contains one:");
            Console.WriteLine(test.Contains(EnumA.One));
            Console.WriteLine("Contains four:");
            Console.WriteLine(test.Contains(EnumA.Four));
            Console.WriteLine();
            Console.ReadLine();

            return;
        }
    }
}
answered 2011-01-06T10:37:34.850

Your Answer