I tried checking for null but the compiler warns that this condition will never occur. What should I be looking for?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming you want to get the value if the key does exist, use
Dictionary<TKey, TValue>.TryGetValue:(Using
ContainsKeyand then the indexer makes it look the key up twice, which is pretty pointless.)Note that even if you were using reference types, checking for null wouldn’t work – the indexer for
Dictionary<,>will throw an exception if you request a missing key, rather than returning null. (This is a big difference betweenDictionary<,>andHashtable.)