Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using System;
  2.  
  3. public class SamplesArray
  4. {
  5.     public static void Main()
  6.     {
  7.         // make a single dimension array
  8.         Array MyArray1 = Array.CreateInstance(typeof(int), 5);
  9.  
  10.         // make a 3 dimensional array
  11.         Array MyArray2 = Array.CreateInstance(typeof(int), 5, 3, 2);
  12.  
  13.         // make an array container
  14.         Array BossArray = Array.CreateInstance(typeof(Array), 2);
  15.         BossArray.SetValue(MyArray1, 0);
  16.         BossArray.SetValue(MyArray2, 1);
  17.  
  18.         int i = 0, j, rank;
  19.         foreach (Array anArray in BossArray)
  20.         {
  21.             rank = anArray.Rank;
  22.             if (rank > 1)
  23.             {
  24.                 Console.WriteLine("Lengths of {0:d} dimension array[{1:d}]", rank, i);
  25.                 // show the lengths of each dimension
  26.                 for (j = 0; j < rank; j++)
  27.                 {
  28.                     Console.WriteLine("    Length of dimension({0:d}) = {1:d}", j, anArray.GetLength(j));
  29.                 }
  30.             }
  31.             else
  32.             {
  33.                 Console.WriteLine("Lengths of single dimension array[{0:d}]", i);
  34.             }
  35.             // show the total length of the entire array or all dimensions
  36.             Console.WriteLine("    Total length of the array = {0:d}", anArray.Length);
  37.             Console.WriteLine();
  38.             i++;
  39.         }
  40.     }
  41. }
  42.  
  43. /*
  44. This code produces the following output:
  45.  
  46. Lengths of single dimension array[0]
  47.     Total length of the array = 5
  48.  
  49. Lengths of 3 dimension array[1]
  50.     Length of dimension(0) = 5
  51.     Length of dimension(1) = 3
  52.     Length of dimension(2) = 2
  53.     Total length of the array = 30
  54. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement