简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM