Introducing Radical.sh

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

Declaring and using single dimensional array in C#

This example shows declaration of single dimensional array and its usage. ( 2 Models)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace ForgetCode
  7. {
  8. public class MainClass
  9. {
  10. public static void Main()
  11. {
  12. // Model 1
  13. int[] myArray1 = new int[5];
  14.  
  15. myArray1[0] = 10;
  16. myArray1[1] = 20;
  17. myArray1[2] = 30;
  18. myArray1[3] = 40;
  19. myArray1[4] = 50;
  20.  
  21. foreach (int i in myArray1)
  22. {
  23. Console.WriteLine(i);
  24. }
  25.  
  26. // Model 2
  27. int[] myArray2 = {1,2,3,4,5};
  28.  
  29. foreach (int i in myArray2)
  30. {
  31. Console.WriteLine(i);
  32. }
  33. }
  34. }
  35. }


Output:
  1. 10
  2. 20
  3. 30
  4. 40
  5. 50
  6. 1
  7. 2
  8. 3
  9. 4
  10. 5