In C#, a List is a versatile and commonly used collection that represents an array whose size can dynamically grow or shrink as needed. It belongs to the System.Collections.Generic namespace, which provides type-safe collections. This tutorial will guide you through understanding how to work with lists in C#, including creating, adding elements, accessing elements, and performing various operations.
A List<T> is a generic collection that can hold elements of any data type specified by the type parameter T. Lists are part of the .NET Framework's collection classes and offer many useful methods for manipulating collections. Here are some key features of lists:
To create a list in C#, you need to include the System.Collections.Generic namespace. Here's how you can declare and initialize a list:
1using System;2using System.Collections.Generic;34class Program5{6static void Main()7{8// Creating a list of integers9List<int> numbers = new List<int>();1011// Adding elements to the list12numbers.Add(1);13numbers.Add(2);14numbers.Add(3);1516// Displaying the list17foreach (int number in numbers)18{19Console.WriteLine(number);20}21}22}
1 2 3
You can access elements in a list using their index. Remember that indices are zero-based:
1using System;2using System.Collections.Generic;34class Program5{6static void Main()7{8List<string> names = new List<string> { "Alice", "Bob", "Charlie" };910// Accessing elements by index11Console.WriteLine(names[0]); // Output: Alice12Console.WriteLine(names[1]); // Output: Bob13Console.WriteLine(names[2]); // Output: Charlie14}15}
Alice Bob Charlie
Lists provide methods to add and remove elements. Here are some common operations:
You can add elements using the Add method or the AddRange method for adding multiple elements at once.
1using System;2using System.Collections.Generic;34class Program5{6static void Main()7{8List<int> numbers = new List<int>();910// Adding a single element11numbers.Add(10);1213// Adding multiple elements using AddRange14numbers.AddRange(new int[] { 20, 30, 40 });1516foreach (int number in numbers)17{18Console.WriteLine(number);19}20}21}
10 20 30 40
You can remove elements using the Remove method to remove a specific element or the RemoveAt method to remove an element at a specific index.
1using System;2using System.Collections.Generic;34class Program5{6static void Main()7{8List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };910// Removing an element by value11fruits.Remove("Banana");1213// Removing an element by index14fruits.RemoveAt(0);1516foreach (string fruit in fruits)17{18Console.WriteLine(fruit);19}20}21}
Cherry
Lists provide methods to search for elements and sort them.
You can use the Contains method to check if a list contains a specific element:
1using System;2using System.Collections.Generic;34class Program5{6static void Main()7{8List<string> colors = new List<string> { "Red", "Green", "Blue" };910// Checking if the list contains an element11bool hasGreen = colors.Contains("Green");12Console.WriteLine(hasGreen); // Output: True1314bool hasYellow = colors.Contains("Yellow");15Console.WriteLine(hasYellow); // Output: False16}17}
True False
You can sort the elements of a list using the Sort method:
1using System;2using System.Collections.Generic;34class Program5{6static void Main()7{8List<int> numbers = new List<int> { 5, 3, 8, 1, 2 };910// Sorting the list11numbers.Sort();1213foreach (int number in numbers)14{15Console.WriteLine(number);16}17}18}
1 2 3 5 8
In the next section, we will explore another important collection type in C#: Dictionary. A dictionary allows you to store key-value pairs and provides efficient ways to access elements based on keys. Stay tuned!