Create Arrays in C#

  • Thread starter Thread starter Krish
  • Start date Start date
K

Krish

Sorry, just starting, so want to make sure I get all this right:


What is the difference between

int[] values = new int[] { 1,2,3,4,5,6 };

vs

int[] values = { 1,2,3,4,5,6 };

The "new" statement is omitted in the second statement.

I know it is the same, but why use one against the other ..

Regards
 
Im starting out too Krish, but according to my book the 'new' way is the
proper but redundant way of doing this HOWEVER it goes on to say that you
would want to use the new method if you want to assign a new array to an
already initialised array reference e.g.

using System;

class demo{

public static void Main(){
int[] nums=new int[] {10,11,12,13};
Console.Write("Numbers:");
Display(nums);

//resize existing array and values
//you could also use... nums=new int[2] {55,64}
nums=new int[] {55,64};

Console.WriteLine();
Console.Write("Numbers:");

Display(nums);
}

private static void Display(int[] n){
foreach (int i in n){
Console.Write(" "+i);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top