Bird
0
0

What does the ContainsKey method do in a C# Dictionary?

easy🧠 Conceptual Q11 of 15
C Sharp (C#) - Collections
What does the ContainsKey method do in a C# Dictionary?
AChecks if a specific key exists in the dictionary
BAdds a new key-value pair to the dictionary
CRemoves a key and its value from the dictionary
DReturns the number of items in the dictionary
Step-by-Step Solution
Solution:
  1. Step 1: Understand the purpose of ContainsKey

    The ContainsKey method checks if a given key is present in the dictionary.
  2. Step 2: Compare with other dictionary methods

    Add adds items, Remove deletes items, and Count returns the number of items, so these are different from ContainsKey.
  3. Final Answer:

    Checks if a specific key exists in the dictionary -> Option A
  4. Quick Check:

    ContainsKey checks key presence [OK]
Quick Trick: ContainsKey checks if key exists before access [OK]
Common Mistakes:
MISTAKES
  • Confusing ContainsKey with Add method
  • Thinking ContainsKey returns a value instead of a boolean
  • Mixing ContainsKey with Count property

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes