It seems these two declarations are the same:
int[] array1 = {11, 22, 33};
and
int[] array2 = new int[] {11, 22, 33};
But what is the need of this part new int[] in the second sample?
Does it make difference?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s no difference in this case – but the first syntax is only available when declaring a variable. From the C# 4 spec section 12.6:
(The “array initializer” is the bit in braces – and an array creation expression is the form where you specify
new int[]or whatever.)When you’re not declaring a local variable or a field (e.g. if you’re passing an argument to a method) you have to use the second form, or from C# 3.0 you can use an implicitly typed array: