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 /
This question was closed Sep 11, 2020 at 06:24 AM by TEEBQNE for the following reason:

Question is off-topic or not relevant

avatar image
0
Question by TEEBQNE · Sep 11, 2020 at 05:41 AM · listreferencedictionary

Quickly access specific list elements in a constantly changing list

I have a list of elements that I have sorted using a custom IComparable. Specific elements in this list are modified quick frequently, so I wanted to store them by reference. I had originally used a Dictionary to store the specific objects hoping they would be stored by reference, but clearly I forgot I'm using c#, so they are stored by value.

The few solutions to my issue were to store the index in the list that these elements are in, the issue is that this index can change. I can update the indexes, but that seems a bit overkill. I can also use the dictionary, then always write out the values in the dictionary to the list, so the values are stored within the dictionary instead. My last idea is because my list is sorted with a Comparable, I can just use a BinarySearch whenever I need an element, but I am somewhat against this as these values can change often and I need to use the data store in them often. Any advice here is appreciated.

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 Bentley · Sep 11, 2020 at 06:20 AM 0
Share

Could you please provide some code snippets showing how you are referencing the dictionary, as well as provide some details about the behavior you expected from the dictionary and the behavior that you got that led you to believe that it was being stored by value? That could help identify the issue.

Another possible point of failure could be if you are using ToList() on your dictionary and then sorting the returned list, which would be a copy of the dictionary. In this case, changes to the individual elements of the list would be reflected in the original dictionary because they would be copies of the references to those objects. If, however, you tried to delete an element from that returned list then that element would not be deleted in the original dictionary. In that case you would need to pull the key out of the list, then delete the element from the original dictionary using that key. If that's also not the solution to your problem then please post some code snippets and descriptions and we can keep working through the problem! Thanks!

1 Reply

  • Sort: 
avatar image
0

Answer by Bentley · Sep 11, 2020 at 05:52 AM

See the Stackoverflow thread here.

If the value in your dictionary is a class then C# will store a reference to the value. The issue that you may be running into is that if you then pass that dictionary to a function, then the function will create a copy of the dictionary to operate on instead of the reference, which can be solved by putting the ref keyword in front of the parameter in the method signature and call. From what I can tell you were correct in trying to use a dictionary first, but without additional details it's hard to pinpoint exactly what went wrong. If I've misunderstood then let me know and I'm happy to keep trying to help!

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 TEEBQNE · Sep 11, 2020 at 06:19 AM 0
Share

The dictionary is never passed anywhere. It is a public variable in a Singleton instance. The instance is never re-instantiated, so I assume there is only the one dictionary that is read/written to.

Follow this Question

Answers Answers and Comments

141 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Reference for List 1 Answer

From two lists into a dictionary... and back? 5 Answers

How to send/copy array from dictionary to class and further to list of class 0 Answers

Combine Children Dictionary in place of Hashtable? 2 Answers


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