Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I can do this with an integer:

int a;
a = 5;

But I can't do this with an integer array:

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

Why not?

To clarify, I am not looking for the correct syntax. That I can look up. I know that this works:

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

Which would be the equivalent of:

int a = 5;

What I am trying to understand is, why does the code fail for arrays? What is the reason behind the code failing to be recognised as valid.

share|improve this question
3  
this can be made possible. you just have to give your idea here. visualstudio.uservoice.com/forums/121579-visual-studio-2015 it may be accepted or not depending on votes and the the amount of work and time developers have to put for it. – M.kazem Akhgary yesterday
1  
@M.kazemAkhgary...Good point. – user2946329 yesterday

7 Answers 7

up vote 15 down vote accepted

The reason there is a difference is that the folks at Microsoft decided to lighten the syntax when declaring and initializing the array in the same statement, but did not add the required syntax to allow you to assign a new array to it later.

This is why this works:

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

but this does not:

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

Could they have added the syntax to allow this? Sure, but they didn't. Most likely they felt that this usecase is so seldom used that it doesn't warrant prioritizing over other features. All new features start with minus 100 points and this probably just didn't rank high enough on the priority list.

Note that { 1, 2, 3, 4, 5 } by itself has no meaning, it can only appear in two places:

  • As part of an array variable declaration:

    int[] a = { 1, 2, 3, 4, 5 };
    
  • As part of an array creation expression:

    new int[] { 1, 2, 3, 4, 5 }
    

The number 5, on the other hand, has a meaning everywhere it appears in C#, which is why this works:

int a;
a = 5;

So this is just special syntax the designers of C# decided to support, nothing more.

This syntax is documented in the C# specification, section 12.6 Array Initializers.

share|improve this answer
    
I could easily see the reason why it only works in that single instance be the fact that anywhere else, it could be confusing to see. Only when you explicitly type your variable, and declare it right there is it completely clear what is happening. – krillgar 19 hours ago
    
A reason for disallowing such syntax is that it's not clear whether int[] arr; for (int i=0; i < 10; i++) { arr = {1,2,3,4,5}; foo(arr); } is intended to create a new array each time through the loop, or is intended to create one array that is used for every loop iteration. If code does arr = new int[] {1,2,3,4,5}; then it's clear that each pass through the loop is intended to create a new object; any legal syntax for creating the array just once will require that its creation be pulled out of the loop. – supercat 17 hours ago

This is the syntax to initialize an int array:

var a = new int[] { 1, 2, 3, 4, 5 };

The int[] is redundant since the type can be determined by the given values in the array. So you can write:

var a = new[] { 1, 2, 3, 4, 5 };
share|improve this answer

To initialize an array you should try like this:

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

Other ways to Initializing a Single-dimensional array:

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

Have a look at this: different ways to initialize different kinds of arrays

share|improve this answer

The reason your array example doesn't work is because of the difference between value and reference types. An int is a value type. It is a single location in memory whose value can be changed.

Your integer array is a reference type. It is not equivalent to a constant number of bytes in memory. Therefore, it is a pointer to the bytes where that data is stored.

In this first line, you are assigning null to a.

int[] a;

In the next line, if you want to change the value of the array, you need to assign it to a new array.

a = new[] {1, 2, 3, 4, 5};

That is why you need the new[] before the list of values within the array if you strongly type your declaration.

int[] a = {1, 2, 3, 4, 5}; // This will work.
var a = {1, 2, 3, 4, 5}; // This will not.

However, as many of the other answers have said, if you declare it in a single line, then you do not need the new[]. If you separate the declaration and initialization, then you are required to use new[].

share|improve this answer
3  
untrue. this is just a syntactic sugar when declaring and initializing variable in one line. you can do it all without new . you can do this int[] a = {1, 2, 3, 4, 5};. and this doesnt mean you are not creating new array. you cant just do it in two lines. the reason is obvious. compiler doesnt like it. – M.kazem Akhgary yesterday
    
If it is so obvious, then tell me why the compiler doesn't like it. I have updated my answer for very clear reasons when and why each are required, and when some will not work. – krillgar yesterday
    
i cant think of anything that conflicts with this syntax. maybe there are another reasons. but its all about syntaxes. nothing else. – M.kazem Akhgary yesterday

{} syntax is available for array initialization, not to be used after declaration.

share|improve this answer

Type and length of arrays should be set in first time:

int[] array = new int[5] { 1 , 2 , 3 , 4 , 5}

if you want to add elements to it later, You can use the lists:

var myList = new List<int>();
myList.Add(1);
myList.AddRange(new [] {2 , 3 , 4 , 5})

You can get result of your lists as an array :

var result = myList.ToArray(); //result is an array of {1,2,3,4,5} values.
share|improve this answer

var a = new int[] { 1, 2, 3, 4, 5 }; - initialization of array with type indicating.

var a = new[] { 1, 2, 3, 4, 5 }; or var a = { 1, 2, 3, 4, 5 }; - initialization of array without type indicating; type is found by type of array's elements.

share|improve this answer
    
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review – Bono yesterday
    
Please add explanation to code-only answers. – Jonathan yesterday
    
@Bono, explanation added – pt12lol 9 hours ago
    
@Jonathan, explanation added – pt12lol 9 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.