Collection initialization syntax in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we create collections using initialization syntax, it's helpful to know how the time to build them grows as we add more items.
We want to understand how the number of items affects the work done during collection setup.
Analyze the time complexity of the following code snippet.
var numbers = new List<int> { 1, 2, 3, 4, 5 };
// This creates a list and adds 5 numbers to it using collection initialization syntax.
This code creates a list and adds each number one by one during initialization.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding each item to the list one at a time.
- How many times: Once for each item in the collection (5 times here).
Each new item means one more add operation. So if you double the items, you double the work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 adds |
| 100 | 100 adds |
| 1000 | 1000 adds |
Pattern observation: The work grows directly with the number of items added.
Time Complexity: O(n)
This means the time to create the collection grows in a straight line with the number of items you add.
[X] Wrong: "Collection initialization adds all items instantly, so time doesn't grow with more items."
[OK] Correct: Each item is added one by one, so more items mean more work and more time.
Understanding how collection initialization scales helps you explain performance when building lists or arrays in real projects.
What if we changed from a List to a LinkedList? How would the time complexity change when initializing with many items?
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
