C# Array dimensional :

One/Single dimensional array :

It is a good practice to use for-loops, when we work with arrays and structures with indices. In the following example we will double the values of all elements of an array of numbers and we will print them:

int[] array = new int[] { 1, 2, 3, 4, 5 };

Example :

  1. /* Description: print array elements */
  2. using System;
  3. namespace DemoProgramming
  4. {
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. int[] array = new int[] { 1, 2, 3, 4, 5 };
  10. int index;
  11. Console.Write("Output: ");
  12. for (index = 0; index < array.Length; index++)
  13. {
  14. array[index] = 2 * array[index];
  15. Console.Write(array[index] + " ");
  16. }
  17. Console.ReadKey();
  18. }
  19. }
  20. }

Output :

C Sharp Language print array elements

Two/Multi dimensional array :

We initialize two-dimensional arrays in the same way as we initialize one-dimensional arrays. We can list the element values straight after the declaration:

int[,] matrix =
{
{1, 2, 3, 4}, // row 0 values
{5, 6, 7, 8}, // row 1 values
};


Example :

  1. /* Description: Printing Matrices */
  2. using System;
  3. namespace DemoProgramming
  4. {
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. int[,] matrix =
  10. {
  11. {1, 2, 3, 4, 5},
  12. {6, 7, 8, 9, 10},
  13. }
  14. for (int row = 0; row < matrix.GetLength(0); row++)
  15. {
  16. for (int col = 0; col < matrix.GetLength(1); col++)
  17. {
  18. Console.Write(matrix[row,col]+" ");
  19. }
  20. Console.WriteLine("\n");
  21. }
  22. Console.ReadKey();
  23. }
  24. }
  25. }

Output :

C Sharp Language Printing Matrices

Array Allocation :

In C# the allocation of memory for the arrays is done dynamically. and arrays are kind of objects, therefore it is easy to find their size using the predefine functions. The variables in the array are ordered and each has an index beginning from 0. Arrays in C# work differently than do in C/C++.


Next Update

Loading...

We adding new programming language tutorials very soon.

We make a Collaboration with HackIsOn YouTube Channel So, Please Subscribe Their Channel and keep supporting