Practice mode C#

What happens when Dictionary.Add is called with a key that is already present?

Correct answer B. An ArgumentException is thrown

Explanation

Dictionary keys must be unique. Add throws ArgumentException if the key already exists, which protects against accidental overwrites. If you intend to replace the value, use the indexer, dict[key] = value, which adds or updates. TryAdd returns false instead of throwing when the key exists.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.