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<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; var firstThreeNumbers = numbers.Take(3); foreach (var number in firstThreeNumbers) { Console.WriteLine(number); }
In this example, a List<int>
is initialized with several integer values, and then the first three items in the list are obtained using the Take
method. The resulting items are then printed to the console using a foreach
loop.
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; var lastThreeNumbers = numbers.Skip(numbers.Count - 3); foreach (var number in lastThreeNumbers) { Console.WriteLine(number); }
In this example, a List<int>
is initialized with several integer values, and then the last three items in the list are obtained using the Skip
method with the Count
property of the list. The resulting items are then printed to the console using a foreach
loop.
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int itemsPerIteration = 3; for (int i = 0; i < numbers.Count; i += itemsPerIteration) { var items = numbers.Skip(i).Take(itemsPerIteration); Console.WriteLine(string.Join(",", items)); }
In this example, a List<int>
is initialized with several integer values, and then the list is iterated through using a for
loop with a specified number of items per iteration. The Skip
and Take
methods are used to get the appropriate items for each iteration, and then the resulting items are printed to the console using string.Join
.
C# get first and last items from List:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; int firstNumber = numbers.First(); int lastNumber = numbers.Last();
Accessing the first and last elements in List C#:
List<string> colors = new List<string> { "Red", "Green", "Blue" }; string firstColor = colors[0]; string lastColor = colors[colors.Count - 1];
LINQ to get first and last item from List in C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; int firstNumber = numbers.FirstOrDefault(); int lastNumber = numbers.LastOrDefault();
Using First() and Last() methods with List in C#:
List<double> prices = new List<double> { 10.5, 20.0, 15.75 }; double firstPrice = prices.First(); double lastPrice = prices.Last();
C# List access first and last element by index:
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" }; string firstFruit = fruits[0]; string lastFruit = fruits[fruits.Count - 1];
Retrieve first and last items from List with LINQ query in C#:
List<string> names = new List<string> { "John", "Jane", "Doe" }; string firstName = names.FirstOrDefault(); string lastName = names.LastOrDefault();
C# List access first and last element using foreach loop:
List<string> items = new List<string> { "Item1", "Item2", "Item3" }; string firstItem = null, lastItem = null; foreach (var item in items) { if (firstItem == null) firstItem = item; lastItem = item; }
Get first and last items from List with Find and FindLast in C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; int firstNumber = numbers.Find(n => true); int lastNumber = numbers.FindLast(n => true);
C# List access first and last element by range:
List<string> items = new List<string> { "Item1", "Item2", "Item3" }; List<string> firstAndLast = items.GetRange(0, 1).Concat(items.GetRange(items.Count - 1, 1)).ToList();
Using ElementAt(0) and ElementAt(count - 1) for first and last item in List C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; int firstNumber = numbers.ElementAt(0); int lastNumber = numbers.ElementAt(numbers.Count - 1);
C# List get first and last elements by Find and FindLast methods:
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" }; string firstFruit = fruits.Find(fruit => true); string lastFruit = fruits.FindLast(fruit => true);
Retrieve first and last item from List with Take and Skip in C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; int firstNumber = numbers.Take(1).FirstOrDefault(); int lastNumber = numbers.Skip(numbers.Count - 1).FirstOrDefault();
C# List access first and last element by Take(1) and Skip(count - 1):
List<string> items = new List<string> { "Item1", "Item2", "Item3" }; string firstItem = items.Take(1).FirstOrDefault(); string lastItem = items.Skip(items.Count - 1).FirstOrDefault();
Get first and last item from List using Enumerable.First() and Last() in C#:
List<double> prices = new List<double> { 10.5, 20.0, 15.75 }; double firstPrice = prices.AsEnumerable().First(); double lastPrice = prices.AsEnumerable().Last();
C# List access first and last element by FirstOrDefault() and LastOrDefault():
List<string> names = new List<string> { "John", "Jane", "Doe" }; string firstName = names.FirstOrDefault(); string lastName = names.LastOrDefault();