Register to Better Coder! It's free.
Enjoy the premium features and succeed at every job interview.

What array initialization syntaxes do you know?

Experience Level: Medior
Tags: .NETC#



int[] array = { 1, 2, 3 }; // Creates array with 3 items

int[] array = new int[] { 1, 2 }; // Creates array wihh 2 items

int[] array = new int[10]; // Creates array with 10 items that have default value 0

var array = new [] { 1, 2, 3 }; // Creates int array with 3 items

For the second and third example the explicit type declaration could be replaced by using var keyword. For the first case the var keyword cannot be used as the information on the right hand side is not enough to infer the type.


No Comments Yet.
Be the first to tell us what you think.