I have a class MyClass
class MyClass { public string Name { get; set; }
from which I want to save multiple instances in the collection. I will often need to check if an instance with a specific name exists, and if that happens, retrieve that instance. Since repeating the entire collection is not an option (performance!), I thought about using a set of key-value pairs, for example. a IDictionary<string, MyClass> .
My program will also allow renaming MyClass instances (this will not allow renaming if the uniqueness of the name is violated). But if I rename a MyClass , I will also need to delete the old entry from the dictionary and add a new one (i.e. With a new name) so that the data is consistent.
The problem is that I will have several such dictionaries (which contain subsets of all instances of MyClass ) everywhere, and it will be difficult to track them and update all dictionaries sequentially after each rename.
Is there a way to automatically support key-value pairs? I think I heard about a data structure that allows this, which exists, at least in C ++ (unfortunately, I have no idea what it's called). In principle, this should be a collection in which the key is not just a simple string, but rather as a link to a string (in this case, the name property), but behaves as if it were a string. Is there such a thing in C #? Do you have other ideas on how to keep the collection consistent?
My only idea is to collect all the dictionaries at the highest level of my program and make a way to rename to update all these dictionaries after the actual renaming process. But there must be a better way!
Why this question is not a duplicate. The best way to change the dictionary key :
I already know that the dictionary does not allow changing the key. Instead, I will ask for another data structure that is somehow compatible with key changes (without loss of performance), and I also ask for other approaches. Therefore, my question is much more open to input from any direction, if it helps to solve the problem of data storage.