Hashtable retrieving value by position?

public static Hashtable Drinks = new Hashtable();
Drinks["Water"] = 3;
Drinks["Coffee"] = 2
Drinks["Beer"] = 5;

To get the value of the element I'm using: int drink = (int) Drinks ["Water"]; which is working.

I wanted to know how I can do this job. card pCardValue1 = (int) [1];

How instead of entering the name of the element I want to get the value by position.

+3
source share
4 answers

The class Hashtable(s Dictionary) are not ordered, that is, its records do not have any order or position, and even if you get a list of keys, the order is random. If you want to get values ​​by key and position, you must use a collection OrderedDictionary.

+3
source

You need to OrderedDictionary. HashTablenot streamlined.

, HashTable 2.0, Dictionary<TKey,TValue> System.Collections.Generic, HashTable .

+3

, OrderedDictionary. , :

var stringKey = "stringKey";
            var stringValue = "stringValue";

            var objectKey = "dateTimeKey";
            var objectValue = DateTime.Now;

            var dict = new OrderedDictionary();
            dict.Add(stringKey, stringValue);
            dict.Add(objectKey, objectValue);

            for (int i = 0; i < dict.Count; i++)
            {
                var value = dict[i];
            }

. : , , , , .

+1

linq, .

int position = 1;
var a1 = (from DictionaryEntry entry in Drinks select entry.Key).Skip(position).FirstOrDefault();

,

object value = 2;
var a = (from DictionaryEntry entry in Drinks where entry.Value.Equals(value) select entry.Key).FirstOrDefault();
0

All Articles