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

前端 未结 9 1377
感情败类
感情败类 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:34

    You should probably use:

    if(myDictionary.ContainsKey(someInt))
    {
      // do something
    }
    

    The reason why you can't check for null is that the key here is a value type.

提交回复
热议问题