Best way to handle a KeyNotFoundException

后端 未结 6 1453
再見小時候
再見小時候 2021-01-31 01:26

I am using a dictionary to perform lookups for a program I am working on. I run a bunch of keys through the dictionary, and I expect some keys to not have a value. I catch the

6条回答
  •  无人及你
    2021-01-31 01:55

    Try using: Dict.ContainsKey

    Edit:
    Performance wise i think Dictionary.TryGetValue is better as some other suggested but i don't like to use Out when i don't have to so in my opinion ContainsKey is more readable but requires more lines of code if you need the value also.

提交回复
热议问题