Bird
0
0

Given a list of tuples representing key-value pairs, how can you initialize a Dictionary using collection initialization syntax in C#?

hard🚀 Application Q8 of 15
C Sharp (C#) - Collections
Given a list of tuples representing key-value pairs, how can you initialize a Dictionary using collection initialization syntax in C#?
Avar dict = new Dictionary<string, int> { ("key1", 10), ("key2", 20) };
Bvar dict = new Dictionary<string, int> { {"key1", 10}, {"key2", 20} };
Cvar dict = new Dictionary<string, int> { "key1" = 10, "key2" = 20 };
Dvar dict = new Dictionary<string, int> { "key1": 10, "key2": 20 };
Step-by-Step Solution
Solution:
  1. Step 1: Understand dictionary initialization syntax

    Dictionary initialization uses braces with each key-value pair inside braces separated by commas.
  2. Step 2: Evaluate options

    var dict = new Dictionary { {"key1", 10}, {"key2", 20} }; uses correct syntax with braces for each pair; others use invalid tuple or indexer syntax.
  3. Final Answer:

    var dict = new Dictionary<string, int> { {"key1", 10}, {"key2", 20} }; -> Option B
  4. Quick Check:

    Dictionary pairs use braces {key, value} in initialization [OK]
Quick Trick: Use braces {key, value} pairs to initialize dictionaries [OK]
Common Mistakes:
MISTAKES
  • Using tuples (key, value) instead of braces
  • Trying to use indexer syntax inside initialization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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