0

Have tried looking into it, but not even sure on how to google this.

I know this gives an error var intArray = {1,2,3,4};and that it has to be

var intArray = new int[]{1,2,3,4}; or int[] intArray = {1,2,3,4};

but I can't seem to find an actual reason WHY I can't use the first one.

Since at the very least I'd expect it to make an object[] by default.

So basically what I'm asking is: Why is the first one incorrect/not allowed.

        var intArray = { 1, 2, 3, 4 }; //incorrect
        var numArray = new int[] { 1, 2, 3, 4 }; //correct
        int[] digArray = { 1, 2, 3, 4 }; //correct

~Suggestions for a better title are welcome, didn't really know how to ask this.~

2 Answers2

2

Your local variable initializer (the var) does not know what type you are giving it when you explicitly defining values.

So this:

var intArray = { 1, 2, 3, 4 };

Would potentially work once you give it a type:

int intArray = { 1, 2, 3, 4 };

But of course we know that you cant store 4 values inside a single int, so need to declare an array.

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

Since at the very least I'd expect it to make an object[] by default.

There are so many variations of what a var = {1,2,3,4} could be that there is no way for your IDE to compile that code and store it in memory. Do you reference it as 1 object? 4 objects? A string value? Is it a JSON schema?

  • This answer doesn't make clear **why** the compiler cannot infer the type of `{ 1 }` while it does for `new[] { 1 }`. For better explanation, see the question referenced as duplicate. – dymanoid Dec 14 '18 at 09:50
0
var intArray = new int[]{}
int[] digArray = {}

These two specify the size of the array. Something that has to be done when creating arrays. Where as

var numArray 

does not.

If you dont want to define the size of an array you can use a list.

JamesS
  • 2,167
  • 1
  • 11
  • 29
  • 2
    Please read Eric Lippert's answer in the question I referenced as duplicate. Technically, this was possible, but the language design team just decided against it. – dymanoid Dec 14 '18 at 09:28
  • @dymanoid Seems like an odd choice to never implement the feature. I suppose we always have lists – JamesS Dec 14 '18 at 09:32
  • @dymanoid thanks, yeah, forgot about the implicit part, most likely would've found that one as well if I'd googled on that... Was too focused on the Array part I suppose. – Dennis Vanhout Dec 14 '18 at 09:42
  • @JamesS your answer isn't really an answer though, since, sure, var doesn't define a size, but neither does int[]. The sizing happens for both of those in the "{,,,}" part. The thing that I just find stupid is that the compiler can't see that the "var" should be replaced with the type "int[]" or at the very least "object[]". – Dennis Vanhout Dec 14 '18 at 09:44
  • 1
    @DennisVanhout [] without a number inside state that the array could be any size. For exmaple int array = new int[5] has a size of 5. int array = new int[] has an unknown size – JamesS Dec 14 '18 at 09:47
  • `int array = new int[5]` is not a valid C# code, neither is `int array = new int[]`. – dymanoid Dec 14 '18 at 09:52
  • Sorry. int[ ] array = new int[ ] and int[ ] array = new int[5] – JamesS Dec 14 '18 at 09:59