views:

94

answers:

4

I'm writing myself a class library to manage Active Directory.

I have an interface:

Public Interface ISourceAnnuaire(Of T as {IGroupe, ITop, IUniteOrganisation, IUtilisateur})
    Readonly Property Changements As Dictionary(Of T, HashSet(Of String))
End Interface

This Changements property is used to save in memory the changes that occur on a particular element that is part of the source.

However, I am stuck with .NET Framework 2.0. What would be the closest .NET 2.0 for HashSet(Of String)?

+2  A: 

I would create my own HashSet class and behind the scenes use a Dictionary with empty values (only use keys).

Meta-Knight
+2  A: 

Either use the non-generic Hashtable or hack a dictionary and use it's key collection.

Public class HashSetHack<T> : //Whatever collection interfaces you need.
{
    private readonly Dictionary<T, object> dict = new Dictionary<T, object>();

    //whatever code you need to wrap the interfaces using dict.Keys eg:

    public void Add(T value)
    {
      dict.add(value, null);
    }
}
Josh Sterling
+1  A: 

Here's a particularly flexible approach:

public abstract class UniqueSet<T, TDictionary> : ICollection<T>
    where TDictionary : IDictionary<T, byte> {

    protected TDictionary _internalDictionary;

    protected UniqueSet(TDictionary dictionary) {
        _internalDictionary = dictionary;
    }

    // implement the ICollection<T> interface
    // using your internal dictionary's Keys property

    // for example:
    public void Add(T value) {
        _internalDictionary.Add(value, 0);
    }

    // etc.

}

public class UniqueSet<T> : UniqueSet<T, Dictionary<T, byte>> {

    public UniqueSet() : base(new Dictionary<T, byte>()) { }

}

Why the abstract base class, you ask? Well, with this approach you could also implement, for example, a SortedUniqueSet<T> with SortedList<T, byte> as its internal collection (and this could implement IList<T>) -- without having to write practically any more code. You could also utilize any fancy other implementations of IDictionary<TKey, TValue> you ever happen to find (if you so chose).

Dan Tao
+1  A: 

The Set<T> class in the Power Collections library behaves almost identically to HashSet<T>. It works with .NET 2.0.

Aaronaught