[.net] Quickie Collection question

Started by
3 comments, last by Telastyn 15 years, 1 month ago
Data structures are not my strong suit. I am looking for a collection that will do this general pattern better than List<T>:

if( Collection.Contains( foo ) ){
    return;
}
Collection.Add( foo );

// process foo.
// recurse.

foo is some arbitrary reference type. Normally I'd go for some list wrapper over a balanced tree, but .NET sorted stuff seems to require pairs and seems not to want to just use the references (or some internal token) as the balance condition. Something in the standard lib I'm missing? Some well known non-standard lib I'm missing? Advise, questions?
Advertisement
I don't have an answer but let me check I understand the question ... you want something which will permit one and only one instance of a particular object (I think this is a 'set' in mathematical terms).
HashSet<T>?
Mike Popoloski | Journal | SlimDX
That looks like exactly the right thing but it is only in 3.5, which implies there is no Framework class for the job in 2.0.

Using a Hashtable<T,object> and setting the value to something (anything :p) would also do the trick but it will clearly be a bit inefficient.
Yes, a set would be acceptable, though I don't necessarily need the behavior of a set. And I am constrained to 2.0 for now, but that looks good. [edit: though this 2.0 compatible lib provides a simple hashset and seems to work well]

Thank you gentlemen.

[Edited by - Telastyn on March 13, 2009 1:49:55 PM]

This topic is closed to new replies.

Advertisement