What if you could create and fill your data collections in just one simple line of code?
Why Collection initialization syntax in C Sharp (C#)? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you need to create a list of database records manually by adding each item one by one in your code.
You write multiple lines to add each record, making your code long and hard to read.
Manually adding each item takes a lot of time and effort.
It is easy to make mistakes like forgetting to add an item or adding duplicates.
Also, the code becomes cluttered and hard to maintain.
Collection initialization syntax lets you create and fill a collection in one simple step.
This makes your code shorter, clearer, and less error-prone.
var list = new List<string>(); list.Add("Record1"); list.Add("Record2"); list.Add("Record3");
var list = new List<string> { "Record1", "Record2", "Record3" };You can quickly set up collections with all needed data in a clean and readable way.
When preparing a list of database table names or query parameters, collection initialization syntax lets you write it all in one place clearly.
Manual item-by-item addition is slow and error-prone.
Collection initialization syntax simplifies creating and filling collections.
It improves code readability and reduces mistakes.
Practice
var list = new List<int> { 1, 2, 3 };Solution
Step 1: Understand collection initialization
Collection initialization lets you create a collection and add items to it in one line.Step 2: Analyze the example code
The codenew List<int> { 1, 2, 3 }creates a list and fills it with 1, 2, and 3 immediately.Final Answer:
Create and fill a collection in one step -> Option BQuick Check:
Collection initialization = create + fill [OK]
- Thinking it only creates empty collections
- Confusing initialization with adding items later
- Assuming type is optional in initialization
Solution
Step 1: Recall dictionary initialization syntax
Dictionaries use curly braces with key-value pairs inside another set of braces:{ {key, value}, ... }.Step 2: Check each option
var dict = new Dictionary { {1, "One"}, {2, "Two"} }; uses correct syntax with nested braces for each pair. Others use invalid syntax for C# dictionaries.Final Answer:
var dict = new Dictionary { {1, "One"}, {2, "Two"} }; -> Option DQuick Check:
Dictionary init = nested braces for pairs [OK]
- Using colon instead of comma between key and value
- Trying to use parentheses instead of braces
- Confusing dictionary syntax with object initializers
var set = new HashSet<int> { 1, 2, 2, 3 };
Console.WriteLine(set.Count);Solution
Step 1: Understand HashSet behavior
A HashSet stores unique elements only, so duplicates are ignored.Step 2: Analyze the initialization
The set is initialized with {1, 2, 2, 3}, but the duplicate '2' is ignored, so the set contains 3 unique elements.Final Answer:
3 -> Option AQuick Check:
HashSet ignores duplicates = count 3 [OK]
- Counting duplicates as separate elements
- Expecting a runtime error due to duplicates
- Confusing HashSet with List behavior
var list = new List<int> { 1, 2, "3" };Solution
Step 1: Check the List type and values
The list is declared as List<int>, so all elements must be integers.Step 2: Identify the incorrect value
The value "3" is a string, not an int, causing a type mismatch error.Final Answer:
Mixing int and string types in List<int> -> Option CQuick Check:
List<int> must contain only ints [OK]
- Assuming implicit conversion from string to int
- Ignoring type mismatch errors
- Thinking semicolon or braces are the problem
Solution
Step 1: Understand nested collection initialization
Each dictionary value is a List<int>, so you must create new List<int> instances inside the dictionary initializer.Step 2: Check syntax correctness
var dict = new Dictionary { { "a", new List<int> {1, 2} }, { "b", new List<int> {3, 4} } }; correctly uses nested initializers: dictionary with key-value pairs, where values are new List<int> with their own initializers.Final Answer:
var dict = new Dictionary { { "a", new List<int> {1, 2} }, { "b", new List<int> {3, 4} } }; -> Option AQuick Check:
Nested collections need explicit new List<int> [OK]
- Omitting 'new List<int>' for nested lists
- Using parentheses or brackets incorrectly
- Trying to use colon syntax inside C# initializers
