Mid Collections

How do you initialize a collection using collection initializers in C#?

Collection initializers allow you to create and populate a collection in a concise way at the

time of declaration.

Example:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

Dictionary<string, int> ages = new Dictionary<string, int>

{ "Alice", 30 },

{ "Bob", 25 }

This syntax internally calls the collection’s Add() method for each element.

More from C# Programming Tutorial

All questions for this course