- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Retrieving Elements from Collection in C#
Let us see an example of a list collection.
We have set the elements −
List<int> list = new List<int>(); list.Add(20); list.Add(40); list.Add(60); list.Add(80);
Now let’s say we need to retrieve the first element from the list. For that, set the index like this −
int a = list[0];
The following is an example showing how to retrieve elements from a list collection −
Example
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); foreach (int val in list) { Console.WriteLine(val); } int a = list[0]; Console.WriteLine("First element: "+a); } }
- Related Articles
- Retrieving Elements from Collection in Java- Iterator
- Retrieving Elements from Collection in Java- ListIterator
- Retrieving Elements from Collection in Java- EnumerationIterator
- Retrieving Elements from Collection in Java- For-each loop
- Retrieving specific documents from collection by _id in MongoDB
- Remove all elements from the Collection in C#
- Retrieving the first document in a MongoDB collection?
- How to retain elements from a Collection in another Collection
- Remove all elements in a collection from a HashSet in C#
- How to remove all elements from a Collection in another Collection
- How to limit the number of records, while retrieving data from a MongoDB collection using Java?
- Copying the Collection elements to an array in C#
- Retrieving ABAP BAdi from SAP BW
- Get the number of elements contained in Collection in C#
- Retrieving data from a table in SAP ABAP

Advertisements