- Home /
Question is off-topic or not relevant
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.
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!
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!
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.