Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Retrieving Elements from Collection in C#
Collections in C# provide various ways to retrieve elements. The List<T> collection is one of the most commonly used collections that allows you to access elements by index, retrieve specific elements, or iterate through all elements.
Syntax
Following is the syntax for accessing elements by index −
ElementType element = list[index];
Following is the syntax for using foreach to iterate through all elements −
foreach (ElementType item in list) {
// process item
}
Using Index-Based Access
You can retrieve elements from a List collection using zero-based indexing. The first element is at index 0, second at index 1, and so on −
using System;
using System.Collections.Generic;
class Demo {
static void Main(string[] args) {
List<int> list = new List<int>();
list.Add(20);
list.Add(40);
list.Add(60);
list.Add(80);
int first = list[0];
int second = list[1];
int last = list[list.Count - 1];
Console.WriteLine("First element: " + first);
Console.WriteLine("Second element: " + second);
Console.WriteLine("Last element: " + last);
Console.WriteLine("Total elements: " + list.Count);
}
}
The output of the above code is −
First element: 20 Second element: 40 Last element: 80 Total elements: 4
Using foreach Loop
The foreach loop is the most convenient way to iterate through all elements in a collection without dealing with indices −
using System;
using System.Collections.Generic;
class Demo {
static void Main(string[] args) {
List<string> cities = new List<string>();
cities.Add("New York");
cities.Add("London");
cities.Add("Tokyo");
cities.Add("Paris");
Console.WriteLine("All cities:");
foreach (string city in cities) {
Console.WriteLine("- " + city);
}
}
}
The output of the above code is −
All cities: - New York - London - Tokyo - Paris
Using LINQ Methods
LINQ provides additional methods to retrieve specific elements from collections −
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main(string[] args) {
List<int> numbers = new List<int> { 10, 25, 30, 45, 50 };
int first = numbers.First();
int last = numbers.Last();
int max = numbers.Max();
int min = numbers.Min();
Console.WriteLine("First: " + first);
Console.WriteLine("Last: " + last);
Console.WriteLine("Maximum: " + max);
Console.WriteLine("Minimum: " + min);
var evenNumbers = numbers.Where(n => n % 2 == 0);
Console.WriteLine("Even numbers:");
foreach (int num in evenNumbers) {
Console.WriteLine(num);
}
}
}
The output of the above code is −
First: 10 Last: 50 Maximum: 50 Minimum: 10 Even numbers: 10 30 50
Comparison of Retrieval Methods
| Method | Use Case | Performance |
|---|---|---|
| Index access [i] | Direct access to specific position | O(1) - Constant time |
| foreach loop | Iterate through all elements | O(n) - Linear time |
| LINQ methods | Query-based element retrieval | O(n) - Linear time |
Conclusion
C# collections provide multiple ways to retrieve elements: direct index access for specific positions, foreach loops for iteration, and LINQ methods for query-based retrieval. Choose the method that best fits your specific use case and performance requirements.
