Click here to Skip to main content
15,904,288 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hi,
What is difference in declaration between these two arrays?
C#
int[] array1 = new int[] { 1, 3, 5, 7, 9 };

and
C#
int[] array1 = { 1, 3, 5, 7, 9 };
Posted
Updated 23-Aug-12 4:56am
v2
Comments
[no name] 23-Aug-12 10:58am    
Running it through your debugger should tell you.

There is no difference.

You will save a lot of time if you disassemble code fragments in question and look at IL code. Even if you don't understand IL, you will be able to figure out what is identical and what is not. Please see:
http://msdn.microsoft.com/en-us/library/f7dy01k1%28v=vs.100%29.aspx[^],
http://en.wikipedia.org/wiki/ILDASM[^].

—SA
 
Share this answer
 
None whatsoever.
The first is just a way to explicitly state what type the compiler should expect. In the later, it works it out from the context.
 
Share this answer
 
Comments
ahsanriaz1K 23-Aug-12 11:04am    
Will the first declaration declares memory in heap and second declaration in stack?
OriginalGriff 23-Aug-12 11:08am    
No - it is an array, so it is a reference type and will always be on the heap.
Christian Graus 23-Aug-12 11:11am    
Like Griff said, it won't change the type of the objects, or how C# deals with them
None, it's just a question of syntax. They are both of the same type, which is what tells the second syntax what to expect.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900