C#


// create a list of type int and add items to it
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);

// create some company objects (used below)
Company company1 = new Company() { Name = "Apple", Symbol = "AAPL" };
Company company2 = new Company() { Name = "Google", Symbol = "GOOG" };

// create a list of type company (complex type) and add an item to it
List<Company> companyList = new List<Company>();
companyList.Add(company1);

// create a list of type Company and assign values to it (via collection initializer)
List<Company> companyList = new List<Company>() { company1, company2 };

// add one more company object to the list
companyList.Add(
  new Company() { Name = "MSFT", Symbol = "Microsoft" }
);

// reference the first item in the list
Console.WriteLine(companyList[0].Symbol);

// loop through the list
foreach (Company co in companyList)
{
  Console.WriteLine(co.Name);
}

// instead of creating the objects and then adding to the list, can add new objects directly
List<Company> companyList = new List<Company>() {
  new Company() { Name = "Apple" }, 
  new Company() { Name = "Google" }
};