Learn

Our first way to create lists and add items took multiple lines:

List<string> citiesList = new List<string>(); citiesList.Add("Delhi"); citiesList.Add("Los Angeles");

We can do it all in one line using object initialization:

List<string> citiesList2 = new List<string> { "Delhi", "Los Angeles" };

We won’t cover everything about object initialization in this lesson, but you do need to recognize and use it.

  • Basic construction uses parentheses ( ) and no values.
  • Object initialization uses curly braces { } and the actual values go in-between.

If we need to add elements to that second list later, we can still use Add():

citiesList2.Add("Kyiv");

Instructions

1.

The current code creates an empty list and uses Add() to add elements.

Delete those lines and make the same list with an object initialization.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?