I have the following declaration:
Dictionary> like = new Dictionary>();
I need to get the first element out, but do not know the key or value. What's the best way to do this?
解决方案
Edit To satisfy the commenters.
Note that to call First here is actually to call a Linq extension of IEnumerable, which is implemented by Dictionary. But for a Dictionary, "first" doesn't have a defined meaning. According to this answer, the last item added ends up being the "First" (in other words, it behaves like a Stack), but that is implementation specific, it's not the guaranteed behavior. In other words, to assume you're going to get any defined item by calling First would be to beg for trouble -- using it should be treated as akin to getting a random item from the Dictionary, as noted by Bobson below. However, sometimes this is useful, a