I want to use a Generic SortedList but I don't need both Key Valuefields

  • Thread starter Thread starter Deckarep
  • Start date Start date
D

Deckarep

What is the best practice technique for using the .net native generic
sorted list when I don't really need to have the value field
occupied.

I'd hate to write something like: mySortedList.Add( "John", null );

Because when I look at that it just doesn't feel right. You don't
exactly know what the intentions of the programmer are. Does he just
not have a value in this instance? Should it be null? If it's null
here can I still put a value if I want?

I just don't like the fact that .net forces you to use the key/value
methodology when you don't always need it.

Or am I just plain old missing some important concept here?

Anyways, what's the ideal way to handle this because like I said I
don't need the value part occupied...it's of no use to me but I still
want to use Generics.

Thanks,

-Deckarep
 
I would happily just use the same value for both - it takes no more
space; you culd try an minimise the last few bytes by using a
SortedList<Foo, bool> or whatever, but I think that makes it even more
unclear what the value is...

string val = "John";
So in your case, mySortedList.Add(val, val);

If the "Add(val,val)" offends you, then in C# 3 you could add a cheeky
extension method:

static class Program
{
static void Main(string[] args)
{
var foo = new SortedList<string, string>();
foo.Add("abc");
}
}
public static class DictionaryExt
{
public static void Add<T>(this IDictionary<T, T> list, T
value)
{
list.Add(value, value);
}
}

Marc
 
[...]
I just don't like the fact that .net forces you to use the key/value
methodology when you don't always need it.

Well, to me, when the key is the value, I put the same reference in both.
You can just as easily use null for the value I suppose, but it feels more
sensible to me to not have non-null values in this situation.
Anyways, what's the ideal way to handle this because like I said I
don't need the value part occupied...it's of no use to me but I still
want to use Generics.

Ideal is in the eye of the beholder. :)

I'm not aware of any .NET collection class, generic or otherwise, that
maintains the elements in sorted order and which does not maintain the key
and value as separate entities. You can sort a simple array after the
fact, of course. But for the data structures that are always in sorted
order, I believe you must provide both a key and a value. You can make
the value null if you like, but to me it seems clearer to use the same
reference for both.

IMHO doing so does make it clear what you intend. It very clearly
indicates that the key being used for the sorting is the entire value
itself, as opposed to sorting values based on some key that is either a
subset of the value, or independent of it altogether.

Pete
 
Deckarep said:
What is the best practice technique for using the .net native generic
sorted list when I don't really need to have the value field
occupied.

Don't use SortedList, basically. It's really badly named, because it's
effectively a map rather than a list.

Use List<T> and then sort it when you need to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top