Collection without duplication  
Author Message
Mazzica





PostPosted: .NET Base Class Library, Collection without duplication Top

I can't find a collection in the framework that doesent allow duplicated values.

is there an alternative

thanks in advance



.NET Development2  
 
 
Mike Danes





PostPosted: .NET Base Class Library, Collection without duplication Top

System.Collections.Hashtable and System.Collection.Generic.Dictionary<K,T> don't allow duplicate keys. Maybe you could use one of those as a collection.


 
 
ahmedilyas





PostPosted: .NET Base Class Library, Collection without duplication Top

you can use a hashtable to store values, with keys. the keys must be unique however but there still could be a duplicate value. You could iterate through each element in the array before adding the value/item in the collection and see if a duplicate exists. However you may find some perf issues doing it this way, since you wouldnt know how many items would be in the collection.

I'll see what else I can dig up, im not entirely sure about a collection which can only contain unique values.



 
 
RizwanSharp





PostPosted: .NET Base Class Library, Collection without duplication Top

Have you seen System.Collections.Generics.Dictionary Its saves a pair of values at each index 1 is Key the orther is Value, Key is Unique and it cant be duplicated if your try to insert a duplicate it'll raise an exception!!!

Best Regards,



 
 
Mazzica





PostPosted: .NET Base Class Library, Collection without duplication Top

Thanks you all for the answers.

I know that dictionaries don't allow duplicated keys. But I need to store a simple collection of string. I need something like a Set collection in java. Strange that MS didn't think about a collection with this behaviour.

In CodeProject, I found a custom set collection, I think the only solution is to create a custom collection. or maybe use a StringCollection and check for values when adding with the contains method.

 
 
CommonGenius.com





PostPosted: .NET Base Class Library, Collection without duplication Top

Try using a KeyedCollection<string, string>, and return item in your GetKeyForItem implementation.