How to use Array.Clear() method to set a range of elements in 3D int array to zero in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,,] arr3D =  { { { 1, 2, 3 }, { 4, 5, 6 } },
                               { { 7, 8, 9 }, { 10, 11, 12 } } };

            //int[,,] arr3D = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },
            //{ { 7, 8, 9 }, { 10, 11, 12 } } };

            Array.Clear(arr3D, 3, 4);

            int bound0 = arr3D.GetUpperBound(0);
            int bound1 = arr3D.GetUpperBound(1);
            int bound2 = arr3D.GetUpperBound(2);

            Console.WriteLine("bound0 = {0}", bound0);
            Console.WriteLine("bound1 = {0}", bound1);
            Console.WriteLine("bound2 = {0}", bound2);
            Console.WriteLine();

            for (int i = 0; i < arr3D.GetLength(0); i++)
            {
                for (int j = 0; j < arr3D.GetLength(1); j++)
                {
                    for (int k = 0; k < arr3D.GetLength(2); k++)
                        Console.Write("{0, 4}", arr3D[i, j, k]);
                }
                Console.WriteLine();
            }
        }
    }
}


/*
run:
 
bound0 = 1
bound1 = 1
bound2 = 2

   1   2   3   0   0   0
   0   8   9  10  11  12

*/

 



answered Apr 11, 2016 by avibootz
...