I have an OrderedDictionary as shown in the below code snippet where keys are strings, integer and character .
Well I am quite new to OrderedDictionary and all I know is that an ordered dictionary can store any type of key/value pairs and we can access values through both index and keys.
OrderedDictionary od = new OrderedDictionary();
od.Add("Key1", "Val1");
od.Add("Key2", "Val2");
od.Add("Key3", "Val3");
od.Add(1, "Val4");
od.Add('k', 'V');
So, I was wondering if I need to access Val4 above, then how should I do it? Because when I am trying to use
Console.WriteLine(od[1]);
it is giving me 'Val2' as it is clearly considering '1' as an index.
Many Thanks!
Python's OrderedDict is a dict subclass that preserves the order in which key-value pairs, commonly known as items, are inserted into the dictionary. When you iterate over an OrderedDict object, items are traversed in the original order. If you update the value of an existing key, then the order remains unchanged.
OrderedDictionary Class represents a collection of key/value pairs that are accessible by the key or index. It is present in System. Collections.
Explicit Interface Implementations Gets a value indicating whether access to the OrderedDictionary object is synchronized (thread-safe).
There is no order. Dictionaries in Swift are an unordered collection type. The order in which the values will be returned cannot be determined. If you need an ordered collection of values, I recommend using an array.
You can cast the integer value of 1 to object to hit the correct indexer.
Console.WriteLine(od[(object)1]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With