Reputation: 337
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?
Upvotes: 0
Views: 206
Reputation: 33833
No, the short answer is that you cannot apply an indexing to a C# Dictionary<>.ValueCollection.
As mentioned in the comments below, the closest C# correlation is using myDict.Values.ElementAtOrDefault(1)
to retrieve the value, since it will safely return the default if the value is not present, which matches the VB.Net behavior. That said, 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.
Upvotes: 4