Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Looping through a LIST with for and foreach in C#

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace forgetCode
  5. {
  6. class program
  7. {
  8. public static void Main()
  9. {
  10.  
  11. List<int> list = new List<int>();
  12. list.Add(1);
  13. list.Add(2);
  14. list.Add(3);
  15.  
  16. foreach (int item in list) // Loop through List with foreach
  17. {
  18. Console.WriteLine(item);
  19. }
  20.  
  21. for (int i = 0; i < list.Count; i++) // Loop through List with for
  22. {
  23. Console.WriteLine(list[i]);
  24. }
  25. }
  26. }
  27. }


Output:
1
2
3
1
2
3