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
I’m not sure what you are about to achieve but here are my answers.
-
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)
-
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))
-
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,
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
Can i make this happen with Dictionary -
Key -TechError
Subj - Alert-Tech Error
Body- Hi, Please find the error details
If i call the key i need to get subj and body. is this possible through dictionary
if yes, what is the syntax for it .