How to change the size of 2D array using the same reference in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[,] arr2d = new string[3, 2];

            arr2d[0, 0] = "c#";
            arr2d[1, 1] = "java";
            arr2d[2, 0] = "c++";

            arr2d = new string[3, 4]; // change the array size

            arr2d[2, 3] = "php";


            int ub0 = arr2d.GetUpperBound(0);
            int ub1 = arr2d.GetUpperBound(1);
            for (int i = 0; i <= ub0; i++)
            {
                for (int j = 0; j <= ub1; j++)
                {
                    if (arr2d[i, j] == null)
                        Console.Write(" . ");
                    else
                        Console.Write(arr2d[i, j] + "    ");
                }
                Console.WriteLine();
            }
        }
    }
}

/*
run:
  
 .  .  .  .
 .  .  .  .
 .  .  . php
  
*/

 



answered Jan 7, 2017 by avibootz

Related questions

2 answers 234 views
1 answer 182 views
1 answer 134 views
134 views asked Jan 2, 2017 by avibootz
2 answers 209 views
1 answer 274 views
...