Mid From PDF Collections C# 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
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details