Object and Collection Initializers
Set properties at creation time concisely.
Object and Collection Initializers is a free C# Academy lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.
What Is an Object Initializer?
An object initializer sets public properties or fields right after new, inside { }, without writing a constructor for each combination.
using System;
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main()
{
var p = new Person { Name = "Ada", Age = 36 };
Console.WriteLine(p.Name + ", " + p.Age);
}
}Initializer Syntax
List property assignments separated by commas inside the braces. They run after the constructor completes.
using System;
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
}
class Program
{
static void Main()
{
var car = new Car
{
Make = "Toyota",
Model = "Corolla",
Year = 2022
};
Console.WriteLine(car.Year + " " + car.Make + " " + car.Model);
}
}Combining Constructor and Initializer
You can call a constructor AND use an initializer. The constructor runs first, then the initializer assigns the listed properties.
using System;
class Widget
{
public string Id { get; }
public string Label { get; set; }
public Widget(string id) { Id = id; }
}
class Program
{
static void Main()
{
var w = new Widget("w1") { Label = "Submit" };
Console.WriteLine(w.Id + ": " + w.Label);
}
}Initializing Only Some Properties
You only list the properties you care about; the rest keep their defaults. This makes initializers flexible.
using System;
class Settings
{
public bool DarkMode { get; set; } = false;
public int Volume { get; set; } = 50;
public string Lang { get; set; } = "en";
}
class Program
{
static void Main()
{
var s = new Settings { DarkMode = true };
Console.WriteLine(s.DarkMode + ", " + s.Volume + ", " + s.Lang);
}
}Collection Initializers
A collection initializer fills a list, set, or array at creation time using { } with comma-separated elements.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var nums = new List<int> { 1, 2, 3, 4, 5 };
Console.WriteLine("Count: " + nums.Count);
Console.WriteLine("Sum: " + Sum(nums));
}
static int Sum(List<int> list)
{
int t = 0;
foreach (var n in list) t += n;
return t;
}
}How Collection Initializers Work
The compiler turns each element into an Add(...) call. Any type with an Add method and IEnumerable supports this syntax.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var names = new List<string> { "Ann", "Bob", "Cara" };
foreach (var n in names)
Console.WriteLine(n);
}
}Dictionary Initializers
Dictionaries can be initialized with key/value pairs in braces, very handy for lookup tables.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var ages = new Dictionary<string, int>
{
["Ann"] = 30,
["Bob"] = 25
};
Console.WriteLine("Ann is " + ages["Ann"]);
}
}Nested Object Initializers
An object initializer can initialize nested objects, building a small object graph in one expression.
using System;
class Address
{
public string City { get; set; }
}
class Customer
{
public string Name { get; set; }
public Address Home { get; set; } = new Address();
}
class Program
{
static void Main()
{
var c = new Customer
{
Name = "Lin",
Home = new Address { City = "Tokyo" }
};
Console.WriteLine(c.Name + " in " + c.Home.City);
}
}Collections of Objects
Combine both features: initialize a list whose elements are themselves built with object initializers.
using System;
using System.Collections.Generic;
class Item
{
public string Name { get; set; }
public decimal Price { get; set; }
}
class Program
{
static void Main()
{
var cart = new List<Item>
{
new Item { Name = "Pen", Price = 1.5m },
new Item { Name = "Book", Price = 9.0m }
};
decimal total = 0;
foreach (var i in cart) total += i.Price;
Console.WriteLine("Total: " + total);
}
}Initializers and init Properties
Object initializers are the way to set init-only properties, since they can only be assigned during initialization.
using System;
class Config
{
public string Env { get; init; }
public int Retries { get; init; }
}
class Program
{
static void Main()
{
var c = new Config { Env = "prod", Retries = 3 };
Console.WriteLine(c.Env + " / " + c.Retries);
}
}Putting It Together
Initializers make construction declarative and readable, especially for configuration objects and test data.
using System;
using System.Collections.Generic;
class Team
{
public string Name { get; set; }
public List<string> Members { get; set; } = new List<string>();
}
class Program
{
static void Main()
{
var t = new Team
{
Name = "Alpha",
Members = { "Ann", "Bob", "Cara" }
};
Console.WriteLine(t.Name + " has " + t.Members.Count + " members");
}
}Quick Check
Test your understanding of initializers.
Recap
Object initializers set public members in { } after new, running after the constructor. Collection initializers add elements via implicit Add calls, and dictionaries support key/value pairs. Both can nest, work with init properties, and make construction concise and declarative.
using System;
using System.Collections.Generic;
class Demo
{
public string Name { get; set; }
public List<int> Nums { get; set; } = new List<int>();
}
class Program
{
static void Main()
{
var d = new Demo { Name = "x", Nums = { 1, 2, 3 } };
Console.WriteLine(d.Name + " " + d.Nums.Count);
}
}Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Object and Collection Initializers” lesson free?
Yes — the full text of “Object and Collection Initializers” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.
What will I learn in “Object and Collection Initializers”?
Set properties at creation time concisely. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Object and Collection Initializers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining Constructors
- Constructor Chaining with this
- Object and Collection Initializers
- Static Constructors