views:

189

answers:

3

Possible Duplicate:
Which collection for storing unique strings?

I am currently using a Dictionary<string, bool> to store a list of unique identifiers. These identifiers do not need to have any data associated with them - I am just using the Dictionary to be able to quickly check for duplicates.

Since I only need keys, and no values, is a Dictionary the way to go here, or is there another collection I don't know about that would be better suited?

+4  A: 

.NET 3.5 includes the HashSet<T> collection type, which sounds like what you want.

Jason
+11  A: 

HashSet<T>

Lee
Which is available in .NET 3.5 and later only.
0xA3
A: 

You can also use a list and the Contains() method. See below:

IList<string> listToTest = new List<string>();

listToTest.Add("testforduplicate");


Console.WriteLine(listToTest.Contains("testforduplicate")); //True
Console.WriteLine(listToTest.Contains("notduplicate")); //False

For custom objects see Programing C#

Koekiebox
He's probably looking for O(1) look-up time.
arul
consider the following:private listToTest = //whatever;public void AddToList(string valToAddParam){ if(this.listToTest.Contains(valToAddParam)){ return; } this.listToTest.Add(valToAddParam);}
Koekiebox
Please go read about Big O notation.
jnylen