Introduction
A List is like a flexible row of boxes where you can store items in order. You can add, remove, or find items easily.
Jump into concepts and practice - no test required
using System; using System.Collections.Generic; public class Program { public static void Main() { List<int> listName = new List<int>(); } }
List<int> numbers = new List<int>(); numbers.Add(10); numbers.Add(20);
List<string> names = new List<string>(); // Empty list with no items yet
List<int> singleItemList = new List<int> { 5 };
List<int> emptyList = new List<int>(); // List is empty, Count is 0
using System; using System.Collections.Generic; public class Program { public static void Main() { List<string> fruits = new List<string>(); Console.WriteLine("Before adding items:"); Console.WriteLine($"Count: {fruits.Count}"); fruits.Add("Apple"); fruits.Add("Banana"); fruits.Add("Cherry"); Console.WriteLine("After adding items:"); Console.WriteLine($"Count: {fruits.Count}"); Console.WriteLine("Items in the list:"); foreach (string fruit in fruits) { Console.WriteLine(fruit); } } }
List<T> in C#?var fruits = new List<string> { "apple", "banana", "cherry" };
fruits.RemoveAt(1);
Console.WriteLine(fruits[1]);List<string> colors = new List<string>();
colors.Add("red");
colors[1] = "blue";
Console.WriteLine(colors[1]);numbers containing {1, 2, 3, 4, 5}, which code snippet correctly doubles each number in the list?