I like using Dictionary, but checking every time if some key exists before indexing it is a nuisance. Is there any built-in data structure in c#, that would return null when a key doesn't exist, instead of raising an error? If there is none, is there some custom class for that? I would like to avoid creating my own class and I'm not sure if this behavior is even possible.
Asked
Active
Viewed 30 times
0
-
`myDictionary.TryGetValue(key, out var value) ? value : defaultValue` in your case `myDictionary.TryGetValue(key, out var value) ? value : null` – Dmitry Bychenko Aug 02 '22 at 07:02
-
@Dmitry Bychenko, yes, but this is ugly, I just want to write myDict["some key"], which is much cleaner and not worry about it raising an error. – Yekoor Aug 02 '22 at 07:05