Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by darthbator · Aug 03, 2012 at 08:25 AM · c#dictionarygenericsnested

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()

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Meltdown · Aug 03, 2012 at 09:52 AM 0
Share

This is a C# specific question and not Unity specific. I'd suggest asking this on C# forums.

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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();


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image darthbator · Aug 03, 2012 at 08:31 PM 0
Share

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#.

avatar image Bunny83 · Aug 04, 2012 at 01:03 AM 0
Share

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()

avatar image
1

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 :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image darthbator · Aug 03, 2012 at 08:28 PM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Nested Dictionary data lost after playmode 0 Answers

Strange Behavior with Dictionary of List of generic Actions 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Accessing a Class's Variables from within a Dictionary Element 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges