C# List Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
Here are some examples of working with lists in C#:
List<string> fruits = new List<string> { "apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew", "kiwi" };
In this example, a List<string>
is initialized with several string values using object initializer syntax.
List<int> numbers = new List<int>(); numbers.Add(1);
In this example, an empty List<int>
is created and an integer value is added to it using the Add
method.
List<int> numbers = new List<int> { 1, 2, 3 }; numbers.Insert(0, 0);
In this example, a List<int>
is initialized with three integer values, and then an integer value is added to the beginning of the list using the Insert
method.
List<int> numbers1 = new List<int> { 1, 2, 3 }; List<int> numbers2 = new List<int> { 4, 5, 6 }; numbers1.InsertRange(0, numbers2);
In this example, two List<int>
objects are created, and then the second list is added to the beginning of the first list using the InsertRange
method.
string numbersString = "1,2,3,4,5"; List<int> numbers = numbersString.Split(',').Select(int.Parse).ToList();
In this example, a comma-separated string of integer values is first split into an array of string values using the Split
method. The resulting array of string values is then converted to a list of integer values using the Select
method and the int.Parse
method.
List<int> numbers = new List<int> { 1, 2, 3, 1, 4, 2, 5, 6, 6 }; var duplicates = numbers.GroupBy(x => x) .Where(g => g.Count() > 1) .Select(g => g.Key); foreach (var duplicate in duplicates) { Console.WriteLine(duplicate); }
In this example, a List<int>
is initialized with several integer values, and then the duplicate values are found using LINQ. The GroupBy
method is used to group the elements by their value, and then the Where
method is used to filter the groups to include only those with more than one element. The resulting groups are then projected to their key value (i.e., the duplicate value) using the Select
method.
List<int> numbers = new List<int> { 1, 2, 3, 1, 4, 2, 5, 6, 6 }; var unique = numbers.Distinct(); foreach (var number in unique) { Console.WriteLine(number); }
In this example, a List<int>
is initialized with several integer values, and then the unique values are found using LINQ. The Distinct
method is used to remove duplicates and return a sequence of distinct elements.
List<int> numbers = new List<int> { 1, 2, 3, 1, 4, 2, 5, 6, 6 }; var duplicateCounts = numbers.GroupBy(x => x) .Where(g => g.Count() > 1) .Select(g => new { Value = g.Key, Count = g.Count() }); foreach (var duplicate in duplicateCounts) { Console.WriteLine($"{duplicate.Value} appears {duplicate.Count} times"); }
In this example, a List<int>
is initialized with several integer values, and then the count of each duplicate value is found using LINQ. The GroupBy
method is used to group the elements by their value, and then the Where
method is used to filter the groups to include only those with more than one element. The resulting groups are then projected to anonymous objects containing the value and count using the Select
method.
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; numbers.Reverse(); foreach (var number in numbers) { Console.WriteLine(number); }
In this example, a List<int>
is initialized with several integer values, and then the list is reversed using the Reverse
method. The resulting list is then printed to the console. Note that the Reverse
method modifies the original list in place, rather than returning a new reversed list.
C# List examples:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
Adding elements to a List in C#:
List<string> fruits = new List<string>(); fruits.Add("Apple"); fruits.Add("Banana");
Removing items from a List in C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; numbers.Remove(3); // Removes the first occurrence of 3
Iterating over a List in C#:
List<string> colors = new List<string> { "Red", "Green", "Blue" }; foreach (var color in colors) { Console.WriteLine(color); }
Sorting a List in C#:
List<int> numbers = new List<int> { 5, 2, 8, 1, 9 }; numbers.Sort(); // Sorts in ascending order
Searching for elements in a List in C#:
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" }; bool containsBanana = fruits.Contains("Banana");
Filtering a List in C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; var evenNumbers = numbers.Where(num => num % 2 == 0).ToList();
Initializing a List in C#:
List<double> prices = new List<double>(new double[] { 10.5, 20.0, 15.75 });
List of objects in C#:
List<Person> persons = new List<Person> { /* List of Person objects */ };
Copying a List in C#:
List<int> originalList = new List<int> { 1, 2, 3 }; List<int> copiedList = new List<int>(originalList);
Clearing a List in C#:
List<string> items = new List<string> { "A", "B", "C" }; items.Clear();