Getting to items in dictionaries

I do like the generics, but sometimes I think Microsoft have tried to simplyfy them to much. They've made it to hard to do the odd things. An example; recently
I was working with a list of name value pairs, so I used a Dictionary:
 Dictionary<string, object>
Everything was going fine, until I realised that in a default situation I needed the first value; regardless of name. I ended up writting a function looking
something like:
private object GetValue()
{
 foreach (KeyValuePair<string,object> item in Values)
 {
  return item.Value;
 }
 return null
}

What I would have prefered to do was something like:
 return values[values.Keys[0]]



Have really got to fix the snippits, so many things depending on them. I wonder if a simply re-install will do it...



Going to TechEd 2006 Boston. Have just booked my trip.Should be fun.

Seems you can check in on the Saturday now, and there's some sort of keynote on the Sunday.

Print | posted on Tuesday, February 07, 2006 4:31 AM

Feedback

# re: Getting to items in dictionaries

left by Eric Newton at 2/8/2006 10:37 PM Gravatar
as for "return values[values.Keys[0]]"
A) what happens when values.Keys.Count<1? the default "generic" implementation had to be deferred to each particular collection.

i do agree that the return statement is more elegant.
Comments have been closed on this topic.