Working with Resize( )


Array size can be changed during the program execution using Resize (ref ArranName,int NewSize ) Method of Array class In C#.NET.


A    0          1        2        3        4        5
1
9
19
29
39
49

Resize(ref A,9)
1
9
19
29
39
49
0
0
0


Example :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAArrays
{
    class ResizeArray
    {
        static void Main()
        {
            int[] A = new int[5] { 9,19,29,39,49 };
            Console.WriteLine("Elements of Array A Are");
            foreach (int i in A)
            {
                Console.Write(i+"   ");

            }
            Console.WriteLine("\nEnter the new size of Array A");
            int s = Convert.ToInt32(Console.ReadLine());
            Array.Resize(ref A,s);
            Console.WriteLine("Elements of Array A After Resizing");
            foreach (int i in A)
            {
                Console.Write(i+"   ");
            }
            Console.ReadLine();
        }
    }
}






































Output:-



No comments:

Post a Comment