What array initialization syntaxes do you know?
Experience Level: Medior
Tags: .NETC#
Answer
Explanation
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.
Related C# job interview questions
Could you explain what the following LINQ methods OrderBy, Where and Single do and what will be the output of the code?
.NETC#LINQ JuniorCould you explain on the following example what a deferred execution is and what materialization is? What will the output be?
.NETC#LINQPerformance MediorHow does the .Max(...) work in LINQ? What will be the result of the following code?
.NETC#LINQ JuniorWhat is a difference between partial class and partial method and what are they good for?
.NETC# Senior