C# Programming - Arrays - Discussion

Discussion :: Arrays - General Questions (Q.No.4)

4. 

If a is an array of 5 integers then which of the following is the correct way to increase its size to 10 elements?

[A].
int[] a = new int[5]; 
int[] a = new int[10];
[B].
int[] a = int[5]; 
int[] a = int[10];
[C].
int[] a = new int[5]; 
a.Length = 10 ;
[D].
int[] a = new int[5]; 
a = new int[10];
[E].
int[] a = new int[5]; 
a.GetUpperBound(10);

Answer: Option D

Explanation:

No answer description available for this question.

Xavier said: (Aug 21, 2012)  
int[] a = new int[5];
int[] a = new int[10];
its declaring it again,

int[] a = int[5];
int[] a = int[10];
neways wrong array is never declared.

int[] a = new int[5];
a = new int[10];
is first declarint the array then giving it the new length..

Gloops said: (Aug 3, 2014)  
Well of course the meaning of the question is which is the correct way to increase the size of the array to 10 elements without loosing the values that are stored in it ?

No correct answer has been proposed.

Chinmay said: (Mar 30, 2015)  
You can use the Resize property Array.Resize. It allocates a new array. It then copies existing element values to the new array.

using System;
class Program
{
static void Main()
{
char[] arr = new char[5];
arr[0] = 'p';
arr[1] = 'y';
arr[2] = 't';
arr[3] = 'h';
arr[4] = 'o';
Array.Resize<char>(ref arr, 6);
arr[5] = 'n';

// Display the array.
Console.WriteLine(new string(arr));
Console.ReadLine();
}
}

Kpman said: (Oct 26, 2015)  
All answers are wrong. No way you could increase the array size in C# with the ways you have given. Once you define a static size, that's it. The proposed answers do not increase the size, they create new objects abandoning the old ones.

So they are not really 'increasing' the size as such. They are created new with a different dimension. I think the question is worded wrong.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.