C Sharp (C#) - CollectionsGiven 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 };Check Answer
Step-by-Step SolutionSolution:Step 1: Understand dictionary initialization syntaxDictionary initialization uses braces with each key-value pair inside braces separated by commas.Step 2: Evaluate optionsvar dict = new Dictionary { {"key1", 10}, {"key2", 20} }; uses correct syntax with braces for each pair; others use invalid tuple or indexer syntax.Final Answer:var dict = new Dictionary<string, int> { {"key1", 10}, {"key2", 20} }; -> Option BQuick Check:Dictionary pairs use braces {key, value} in initialization [OK]Quick Trick: Use braces {key, value} pairs to initialize dictionaries [OK]Common Mistakes:MISTAKESUsing tuples (key, value) instead of bracesTrying to use indexer syntax inside initialization
Master "Collections" in C Sharp (C#)9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More C Sharp (C#) Quizzes Classes and Objects - Object instantiation with new - Quiz 8hard Classes and Objects - Constructor overloading - Quiz 3easy Exception Handling - Why exception handling is needed - Quiz 12easy File IO - Why file operations matter - Quiz 2easy File IO - File paths and Directory operations - Quiz 3easy File IO - Using statement with file streams - Quiz 12easy Inheritance - Why inheritance is needed - Quiz 4medium Interfaces - Interface vs abstract class decision - Quiz 8hard Interfaces - Implementing interfaces - Quiz 6medium Properties and Encapsulation - Read-only and write-only properties - Quiz 7medium