C# Programming - Arrays - Discussion

Discussion Forum : 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?
int[] a = new int[5]; 
int[] a = new int[10];
int[] a = int[5]; 
int[] a = int[10];
int[] a = new int[5]; 
a.Length = 10 ;
int[] a = new int[5]; 
a = new int[10];
int[] a = new int[5]; 
a.GetUpperBound(10);
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
4 comments Page 1 of 1.

Kpman said:   10 years ago
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.

Chinmay said:   1 decade ago
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();
}
}

Gloops said:   1 decade ago
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.

Xavier said:   1 decade ago
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..

Post your comments here:

Your comments will be displayed after verification.