CRK CRK - 1 year ago 131 Question

Dictionary Difference between C# and VB.Net

Why if I have a

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download