What is the best way to index value by two keys in a dictionary. Ex.: having students with unique Id(integer) and username(string) create a dictionary that holds objects of type student indexed by Id and username. Then on a retrieval use either Id or username.
Or may be Dictionary is not a good match?
P.S. I’m aware of tuples but failing to see how they can be used in this scenario.
EDIT: As an alternative to using two keys, I could create a string representation of both id and username separated by unique divider, and then match keys using regex?! Ex.: “1625|user1”
Since you want to be able to retrieve by either, you’ll need two dictionaries.
Assuming the
Studenttype is a reference type, you’ll still only have oneStudentobject per student, so don’t worry about that.It would be best to wrap the dictionaries in a single object:
And so on.