Loops


Intro

My notes on various ways to loop over objects.

 


A Substitute for the “For” loop

 

Create a list of “Count” objects of type T.

List<T> myList = Enumerable.Range(1, Count).Select(_ => new T).ToList();

 


Foreach

 

Simple FOR construct that iterates over each itme in a list

List<int> numbers = new List<int> {1, 2, 3, 4, 5, 6, 7, 8}; foreach (var num in numbers) { Console.WriteLine(num); }

DotNetFiddle example: https://dotnetfiddle.net/soZoXr