C# Programming Language: How to Traverse a List

Creating List

using System.Collections.Generic;
List<int> myList = new List<int>();

Adding Elements Into The List

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
Console.WriteLine(String.Join(" ", myList));
Output:
1 2 3

Traversing a List

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
for(int i = 0; i < myList.Count; i++)
{
Console.WriteLine(myList[i]);
}
Output:
1
2
3
List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
foreach(var element in myList)
{
Console.WriteLine(element);
}
Output:
1
2
3

Conclusion

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Deploying multi node kubernetes environment in your local machine.

HAProxy configuration Using Ansible

Some OOPs Interview Questions

Why extract methods when modifying legacy code?

What’s Inside Milvus 1.0?

Collection of Linux fortunes ❤

Assertive Programming in R

Profiling with Java Agents — Part 2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jesse L

Jesse L

More from Medium

C# Programming Language: Lists

Async programming vs Parallel programming (Basic)

C# extension methods

Singleton Pattern