What happens to C# Dictionary lookup if the key does not exist?

前端 未结 9 1243
感情败类
感情败类 2021-02-01 00:09

I tried checking for null but the compiler warns that this condition will never occur. What should I be looking for?

9条回答
  •  庸人自扰
    2021-02-01 00:30

    int result= YourDictionaryName.TryGetValue(key, out int value) ? YourDictionaryName[key] : 0;
    

    If the key is present in the dictionary, it returns the value of the key otherwise it returns 0.

    Hope, this code helps you.

提交回复
热议问题