0

I see from this question that there are four permissible ways to initialise an array in C#. Taken from the accepted answer:

string[] array = new string[2]; // creates array of length 2, default values
string[] array = new string[] { "A", "B" }; // creates populated array of length 2
string[] array = { "A" , "B" }; // creates populated array of length 2
string[] array = new[] { "A", "B" }; // created populated array of length 2

The linked answer clarifies that you can also make the above expressions terser, with the exception of the third, using the var keyword:

var array = new string[2]; // creates array of length 2, default values
var array = new string[] { "A", "B" }; // creates populated array of length 2
string[] array = { "A" , "B" }; // creates populated array of length 2
var array = new[] { "A", "B" }; // created populated array of length 2

My question is: why would you ever wish to use the new syntax for an array? Is there any performance benefit to using either var array = new string[] { "A", "B" } instead of string[] array = { "A" , "B" }?

Lou
  • 2,200
  • 2
  • 33
  • 66
  • 1
    Maybe I don't know what data will be added to my array. – SᴇM Dec 11 '20 at 10:36
  • 2
    `string[] array = { "A" , "B" };` is still [using `new` under the hood](https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQBMQGoA+ABATARgFgAobAZgAI8KBhCgbxIuasuwBYKBZACgEoGTFsOz4ADAG0AuhQgIEEAJ4UAvAwoAiAIIaKSTQCFdAXwDcQ5sZLGgA===) – Pavel Anikhouski Dec 11 '20 at 10:37
  • 2
    In your example, there is no advantage, that's why they made a change, to initialize it without `new`. – SᴇM Dec 11 '20 at 10:37
  • @SᴇM But you don't have to initialise the array in either syntax, no? You can declare `string[] array;` or `var array = new string[]` or `string array = new string[]` without initialisation? – Lou Dec 11 '20 at 10:38
  • 1
    @Lou you can't, you need to specify the size of the array. – SᴇM Dec 11 '20 at 10:38
  • 1
    Also - to the close- and/or down-voter - I'm not sure what about this question is opinion based? I'm not asking which syntax is prettier, I'm interested in whether there is a measurable performance difference or other reason why omitting new would be inadvisable. – Lou Dec 11 '20 at 10:40
  • 3
    Often when a language has multiple ways to achieve the same goal, the simpler forms came later. Don't expect a *performance* difference, merely a *developer productivity* one. – Damien_The_Unbeliever Dec 11 '20 at 10:41
  • 5
    This is not really an advantage/disadvantage thing, first, it was with `new`, then with newer c# versions, developers thought, why shouldn't we remove the `new` keyword from array initialization (for simplicity/less code sake), then they removed it. That's it. – SᴇM Dec 11 '20 at 10:42
  • I don´t see why one is better than the other. This question is pretty opinion-based. – MakePeaceGreatAgain Dec 11 '20 at 10:44
  • @HimBromBeere - From a C# beginner perspective though, I don't know whether one is better than the other. I'm not proffering or soliciting an opinion on what's better stylistically, I was interested in whether there was a performance benefit to using one or the other, which I imagine is measurable. I regularly see many such questions in [python] which are not considered opinion-based, as they can be answered with a `%timeit` call. – Lou Dec 11 '20 at 10:49
  • @SᴇM - That makes sense. I wasn't sure whether this fell into the category of 1. Different syntaxes that are suitable for different situations or 2. Different syntaxes ... just because. I guess it falls into the latter category. – Lou Dec 11 '20 at 10:50
  • Well, closed for whatever reason does not mean your question is "bad". It just means: "it can´t be answered", or at least not within a single, terse answer. – MakePeaceGreatAgain Dec 11 '20 at 10:50
  • But it just was answered? – Lou Dec 11 '20 at 10:51
  • Well, many answers are not really answers in the sense of "whrong/right". In your specific case answers will likely be just *opinions* or *preferences personal*, not really *facts*. Chances are, those preference suffice your needs, but they don´t neccesarily do for the entire community. – MakePeaceGreatAgain Dec 11 '20 at 10:53
  • Okay. I've reworded the question to focus on performance. I believe this is more answerable. – Lou Dec 11 '20 at 10:57
  • If you want performance, consider allocating (small) arrays on the stack using [stackalloc](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/stackalloc). – l33t Dec 11 '20 at 21:41

1 Answers1

-2

Because with new you can specify array type, i.e. you have this code

var array = new[] { 1, 3, 5 }

this will be int, but you want it to be array of byte, so you can do it only with new. Also you can have array declared somewhere as class field string[] array, an initialization of it somewhere in constructor, so you will only see array = { "A", "B" } which isn't clear

SelfishCrawler
  • 225
  • 2
  • 12
  • Yeah but as I said, int[] and new declaration of same array can be in absolutely different places of code, and you aren't covering this example - assume you define your array at the beginning of the file and your constructor is at line 10000. – SelfishCrawler Dec 11 '20 at 11:03
  • You cannot definitely tell what answer this question "at all". Because the reason may vary on the situation and code present – SelfishCrawler Dec 11 '20 at 11:05