Can a key be of dictionary datatype which means can there be a dictionary inside dictionary

1.Can a key be of dictionary datatype which means can there be a dictionary inside dictionary
2. Can a key have multiple values in a dictionary
3. Can a value have multiple keys inside dictionary
Please give your solution if it is correct

Check this


(or)

Thanks
Ashwin S

Hi @sravya_vankadaru,

I’m not sure what you are about to achieve but here are my answers.

  1. Yes, a key or a value of a Dictionary can be of any type. Meanwhile I don’t see a proper use case to have a Dictionary as a key but it’s possible. For example if you need to create a Dictionary with a key of Dictionary (String key, String value) type and an Object value you’ll have to declare a variable of type: Dictionary<Dictionary<String, String>, Object>. To instantiate this variable you’ll use: New Dictionary(Of Dictionary(Of String, String), Object)

  2. Yes, I think that this use case is more common. If you want a key being associated with multiple values, the easiest way is to have a Dictionary with a value of List type (or even of Dictionary type, in this case you’ll have a Dictionary inside another Dictionary).
    For example a Dictionary with a String key and a List of Integer value: Dictionary<String, List<Int32>>. To instantiate this variable you’ll use: New Dictionary(Of String, List(Of Int32)).
    Other example, a Dictionary with a Int32 key and a Dictionary (String key, String value) as value. You’ll need to declare a variable of type: Dictionary<Int32, Dictionary<String, String>>. Instantiation would be: New Dictionary(Of Int32, Dictionary(Of String, String))

  3. Yes, it simply means that you have multiple entries in your Dictionary containing the same values (for different keys). I think that here your goal is to group entries by key. You may do that by running Linq queries on your Dictionary.

Let me know if it suit your needs.

Regards,

@Masire

Hi,

Thanks for putting all your efforts to answer my question. I specifically don’t achieve anything but these questions were asked in an interview. So thought of finding the answer. Thanks

Hi,

No problem. It may be useful for someone else.

Regards,

@Masire