General answer is that a) in C# you can achieve the same result in few ways
and b) not all ways were available from the beginning.
For b) there are two parts:
- code that has been written before new ways were added
- new code written with old habbits - new ways are not always better than the old ones, they just look better :)
Re: array
Have a look at Array Initialization in Single-Dimensional Arrays (C# Programming Guide).
Object and Collection Initializers (C# Programming Guide) is also relevant.
Also, in Array Creation from new operator (C# reference) shows 3 ways (and that's not all the ways):
var a = new int[3] { 10, 20, 30 };
var b = new int[] { 10, 20, 30 };
var c = new[] { 10, 20, 30 };
There even is a Question here about all possible ways: All possible array initialization syntaxes.
Re: new int
There are two parts in the answer:
- With
int i; (just a declaration) you cannot use before you initialise it.
- There are multiple ways to initialise variables, as with arrays.
Have a look at Initializing value types in Value types (C# Reference).
You can read there:
You can, of course, have the declaration and the initialisation in the same statement as in the following examples:
int myInt = new int();
–or–
int myInt = 0;
For default values specifically, have a look at Default values table (C# reference). You can see there that you can also:
int a = default(int);
and from C# 7.1 even
int a = default;