I've noticed a strange VB.NET thing. Coming from this question I've provided a way to access keys and values of dictionaries' KeysCollection
and ValuesCollection
via index to get, say, the first item. I know that it makes only sense in a SortedDictionary
since a normal Dictionary
is not ordered (well, you should not rely on its order).
Here's a simple example:
Dim sortedDict As New SortedDictionary(Of DateTime, String)
sortedDict.Add(DateTime.Now, "Foo")
Dim keys As SortedDictionary(Of DateTime, String).KeyCollection = sortedDict.Keys
Dim values As SortedDictionary(Of DateTime, String).ValueCollection = sortedDict.Values
Dim firstkey As DateTime = keys(0)
Dim firstValue As String = values(0)
But I was surprised that the question's asker said that it doesn't compile whereas it compiles and works for me without a problem:
System.Diagnostics.Debug.WriteLine("Key:{0} Value:{1}", firstkey, firstValue) ' Key:04/29/2016 10:15:23 Value:Foo
So why can I use it like there was an indexer if there isn't actually one in SortedDictionary(Of TKey, TValue).KeyCollection
-class and also none in the ValueCollection
. Both implement ICollection<T>
which is the parent interface of IList<T>
. So you can loop it and it has a Count
property, but you can't access items via index as I do above.
Note that it's a fresh console application with no extensions inside. I can't go to the definition of the indexer either(also not with resharper). Why does it work for me?
Side-note: it doesn't work in C#. I get the expected compiler error:
Cannot apply indexing with [] to an expression of type 'SortedDictionary.KeyCollection'
var dict = new SortedDictionary<DateTime, string>();
dict.Add(DateTime.Now, "Foo");
DateTime dt = dict.Keys[0]; // here
Here's a screenshot of the compiling VB.NET code: