- Home /
C#: Worlking with nested Dictionaries
Hey guys I wanted to form a fairly simple nested dictionary as follows.
private Dictionary<string, Dictionary<string, object>> partyMembers = new Dictionary<string, Dictionary<string, object>>();
However I am a little confused as to how to best access and cast the stores values inside the nested dictionary. What is the correct method of doing this? Do I get into it as such?
dictionary[key.string.outer[key.string.inner]]
Because that ^ does not seem to work. Do I access it like this?
dictionary[key.string.outer][key.string.inner]
How about casting and calling methods from the objects inside the deepest nested dictionary. In this case you can see I am nesting dictionaries of generic objects that will need to be cast in the deepest layer. Can I call stuff out of these as follows?
(type)dictionary[key.string.outer][key.string.inner].method(); || (type)dictionary[key.string.outer[key.string.inner]].method()
This is a C# specific question and not Unity specific. I'd suggest asking this on C# forums.
Answer by Bunny83 · Aug 03, 2012 at 11:44 AM
Speed is right.
dictionary[key]
will return the value for the given key. The value is another dictionary. You could call any function of that inner dictionary as well as the indexer
dictionary[outerKey][innerKey]
This returns the value for the genen key from an nested dictionary. Since it's of type object you have to cast it like you've already mentioned. The problem is the order of operators. The unary cast operator follows the dot-operator and the function call.
That's why you have to either use a temp variable which is most the time the cleaner way, or use brackets:
( (MyType)dictionary[outerKey][innerKey] ).MethodOfMyType();
Since that looks messy it's better to write:
MyType tmp = (MyType)dictionary[outerKey][innerKey];
tmp.MethodOfMyType();
I was planning on caching script components from players party actors inside this particular data structure. I figured I might as well ask. Is it going to actually be slower and less efficient to pull those script objects out of the dictionary then it would be to continually use playerActor.gameObject.GetComponent ?
If nothing else this has been a good lesson on working with nested dictionaries and general casting in C#.
Well, it depends on how many components are on the gameobject ;) GetComponent isn't that slow. I can't say which one is faster, but it seems that are two totally different things. You need a gameObject reference to call GetComponent, so why don't you store a reference to the component in the first place?.
In most cases when an object have a custom script attached, it's better to work with references of this type ins$$anonymous$$d of GameObject references. You can get the gameObject from every component with the .gameObject property.
btw: The type Component also have the GatComponent functions, so you don't need to write playerActor.gameObject.GetComponent(). It's enough to write: playerActor.GetComponent()
Answer by homeros · Aug 03, 2012 at 10:38 AM
Correct way is to call it is the second method you wrote:
dictionary[key.string.outer][key.string.inner]
and you can try these very easily. just write them on unity instead of writing here :)
I was trying them out in Unity the issue was that I was unsure how to get information out of the uncast objects stored in the deepest dictionary. Therefore I didn't really know how to even check if they where being populated =P
It looks like Bunny83 managed to answer my questions about casting those generic objects so I should be good to go now! Thanks a lot for the help!