Reputation: 161
Why do I have to write int
after new
when I am declaring the array num
without an initializer? E.g.
int[] num = new int[5];
For another array, num1,
I just declared it first and then gave it values, and I didn't type int
after new
like I did for array num
.
int[] num1 = new [] {1, 2, 3, 4};
Why does this shortcut not work for my first example?
Upvotes: 10
Views: 5381
Reputation: 32790
Because you seem to expect that the way the compiler infers the type of the array is somehow based on the type to the left of the assignment operator:
int[] num = new[5];
Your thought process is:
num
is an array of int
s.new[5]
is an array of something.int
because that is num
's type.That is not the way the compiler works. The compiler must figure out univocally the type to the right of the assignment expression and once it does, it will check if the assignment to whatever is on the left is valid.
In the syntaxes allowed in C#, the compiler always has enough information to figure out the type on the right side of the assignment (if it has any). Your proposed syntax wouldn't provide that information.
Upvotes: 3
Reputation: 103555
The "full" syntax for declaring an array with values is new int[] {1, 2, 3, 4};
.
Your new [] {1, 2, 3, 4};
line is a shorthand syntax. It's an "implicitly typed" array. Because the values are all int
, the array is automatically declared an int[]
.
The reason you can't also write int[] x = new [5];
is simply that this feature has not been implemented by the C# designers.
Upvotes: 9
Reputation: 203850
The type of the array can be inferred (under certain circumstances, which your example fullfills) if you provide the values for the array. Since the values are all integers, the compiler is able to infer that you want an array of integers. If you don't provide any values (or if the values aren't all implicitly convertible to the type of one of the values), it has no way of inferring what the type of the array should be, and so you need to specify it explicitly.
Upvotes: 10
Reputation: 157136
why I have to write int after new when I am declaring array num with default values
You don't. There are a number of shorthands possible because often the compiler can infer the type of the array you are creating, like in your case.
So this is a perfectly fine declaration:
var num1 = new[] { 1, 2, 3, 4 };
See, there is no int
here, but if you hover over var
it will tell you the array is an int[]
.
It is getting harder when you are mixing types:
var num1 = new [] { "1", 1 };
Then the compiler doesn't know and forces you to specify a type.
Upvotes: 0
Reputation: 12815
In your first scenario, you're declaring an empty array, so you need to determine what type it is.
int[] num = new int[5]; // New empty array of integers with 5 slots.
However, in your second example, the type of the array is implied based on the values that you assign to the slots in the array.
int[] num1 = new [] {1, 2, 3, 4}; // New array of integers with 4 slots, already assigned.
The above works because every element in the array is of the same type.
If you were to do the following, a compilation error would occur because the type can't be inferred as the elements are different types.
var array = new [] {1, "something"}; // This won't compile.
But, if you declare one of the elements as a common base type, then it will store all of the elements as that base type.
var array = new [] {(object)1, "something", DateTime.Now}; // This is an array of objects.
Upvotes: 2