Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Premium
Register
Login
How to get all items in a C# List
Mahesh Chand
Jul 06, 2012
196.1k
0
1
WhatsApp
How to read a list items using C#.
Reading List Items
The List<T> is a collection. We can use the foreach loop to go through all the items and read them. The following code snippet reads all items of a list and prints them on the console.
foreach (string author in AuthorList)
{
Console.WriteLine(author);
}
We can also use a var keyword for any kind of data type.
foreach (var author in AuthorList)
{
Console.WriteLine(author);
}
The following code snippet creates a new list and reads all of its items and displays on the console.
public void CreateList()
{
// Create a list of strings
List<string> AuthorList = new List<string>();
AuthorList.Add("Mahesh Chand");
AuthorList.Add("Praveen Kumar");
AuthorList.Add("Raj Kumar");
AuthorList.Add("Nipun Tomar");
AuthorList.Add("Dinesh Beniwal");
// Read all data
Console.WriteLine("Authors List");
foreach (var author in AuthorList)
{
Console.WriteLine(author);
}
}
Download Free book:
Programming List with C#
C# list
C# list items
get all List items
ListT items
read all list items
read C# list
Read list
Recommended Free Ebook
Pro WPF: Windows Presentation Foundation in .NET 3.0
Download Now!
Next Recommended Readings