CRK CRK - 1 month ago 7
Vb.net Question

Dictionary Difference between C# and VB.Net

Why if I have a

Dictionary<string, MyClass> myDic
in C# I can't do
myDic.Values[1]
because it can't index a Dictionary instead of Vb.Net that allow me to do
myDic.Values(1)
? Is there any difference between C# Dictionary and a VB.Net one?

Answer

No, the short answer is that you cannot apply an indexing to a C# Dictionary<>.ValueCollection.

As Dmitry mentioned in his comment, you can instead use myDic.Values.ElementAt(0) to retrieve the value, although this may be indicative of misusing a dictionary, particularly since order is not guaranteed.

Typically, the most efficient and valuable way to retrieve values from a Dictionary is by key lookup. This is merely speculation, but it wouldn't surprise me if indexing on the Values Collection was not supported in C# simply to guide you in the right direction and prevent abuse.