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> names = new List<string> { "Alice", "Bob", "Charlie", "Dave" }; string secondName = names[1]; Console.WriteLine(secondName); // Output: "Bob"
In this example, a List<string>
is initialized with several string values, and then the second item in the list is accessed using the square bracket notation and index value 1
.
List<string> list1 = new List<string> { "Alice", "Bob", "Charlie", "Dave" }; List<string> list2 = new List<string> { "Bob", "Eve" }; var itemsNotInList2 = list1.Except(list2); foreach (var item in itemsNotInList2) { Console.WriteLine(item); }
In this example, two List<string>
objects are created, and then the items in list1
that are not in list2
are found using the Except
method. The resulting items are then printed to the console.
List<string> list1 = new List<string> { "Alice", "Bob", "Charlie", "Dave" }; List<string> list2 = new List<string> { "Bob", "Eve" }; var commonItems = list1.Intersect(list2); foreach (var item in commonItems) { Console.WriteLine(item); }
In this example, two List<string>
objects are created, and then the items in list1
that are also in list2
are found using the Intersect
method. The resulting items are then printed to the console.
C# get items from List:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; int specificNumber = numbers[2]; // Retrieves the item at index 2
Retrieve elements from List in C#:
List<string> colors = new List<string> { "Red", "Green", "Blue" }; string firstColor = colors[0]; // Retrieves the first item in the list
Accessing List elements by index in C#:
List<double> prices = new List<double> { 10.5, 20.0, 15.75 }; double secondPrice = prices[1]; // Retrieves the second item in the list
Getting items from List using LINQ in C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; int specificNumber = numbers.FirstOrDefault(n => n == 3); // Retrieves the first item matching the condition
Retrieve multiple items from List in C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; List<int> selectedNumbers = numbers.Where(n => n % 2 == 0).ToList(); // Retrieves items based on a condition
Accessing List elements with foreach loop in C#:
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" }; foreach (var fruit in fruits) { Console.WriteLine(fruit); // Access elements using foreach loop }
C# List element retrieval by value:
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" }; string banana = fruits.Find(fruit => fruit == "Banana"); // Retrieves item by value
Finding items in List with Where in C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; List<int> selectedNumbers = numbers.Where(n => n > 2).ToList(); // Retrieves items based on a condition
C# List element access by index:
List<string> colors = new List<string> { "Red", "Green", "Blue" }; string secondColor = colors.ElementAt(1); // Retrieves the item at index 1
Getting the first item from List in C#:
List<double> prices = new List<double> { 10.5, 20.0, 15.75 }; double firstPrice = prices.First(); // Retrieves the first item in the list
C# List element access by LINQ query:
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" }; string specificFruit = fruits.First(fruit => fruit.StartsWith("B")); // Retrieves the first item matching the condition
Retrieving specific items from List in C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; List<int> selectedNumbers = numbers.FindAll(n => n % 2 == 0); // Retrieves items based on a condition
C# List element access by condition:
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" }; string specificFruit = fruits.Find(fruit => fruit.Length > 5); // Retrieves the first item matching the condition
Getting last item from List in C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; int lastNumber = numbers.Last(); // Retrieves the last item in the list
C# List element access with Find method:
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" }; string specificFruit = fruits.Find(fruit => fruit.Contains("a")); // Retrieves the first item matching the condition
Retrieving a range of items from List in C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; List<int> selectedNumbers = numbers.GetRange(1, 3); // Retrieves a range of items starting from index 1
C# List element access by predicate:
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" }; string specificFruit = fruits.Find(fruit => fruit.StartsWith("B")); // Retrieves the first item matching the condition