C# Arrays and Lists
An array has a fixed size. A List<T> can grow and shrink, and is used far more often.
ExampleC#
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int[] nums = { 10, 20, 30 };
Console.WriteLine(nums[1]);
Console.WriteLine(nums.Length);
List<string> cars = new List<string>();
cars.Add("Volvo");
cars.Add("BMW");
cars.Add("Ford");
cars.Remove("BMW");
cars.Sort();
Console.WriteLine(cars.Count);
Console.WriteLine(string.Join(", ", cars));
Dictionary<string, int> ages = new Dictionary<string, int>();
ages["Ada"] = 36;
ages["Sam"] = 22;
Console.WriteLine(ages["Ada"]);
Console.WriteLine(ages.ContainsKey("Lee"));
}
}Output
20 3 2 Ford, Volvo 36 False
Quick check
Which type is a resizable list in C#?
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.