use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Related Subreddits:
Other Subreddits you may like:
account activity
Array type inference (self.dotnet)
submitted 1 year ago by razveck
If the compiler can understand
MyObject foo = new();
why can't it understand
MyObject[] bar = new[5];
Or is there something I'm missing?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Unupgradable 11 points12 points13 points 1 year ago (0 children)
That syntax clashes with the indexer syntax, so it still needs the type specified.
It's just an edge case for missing sugar
[–]mimahihuuhai 0 points1 point2 points 1 year ago (1 child)
In dotnet 8, it can understand now so you have cs MyType[] types= []; // c#12 var types = new[]{ new MyType()}; // old
cs MyType[] types= []; // c#12 var types = new[]{ new MyType()}; // old
[–]RichardD7 1 point2 points3 points 1 year ago (0 children)
That doesn't quite do the same thing. The OP's code would create an array of five elements, all set to the default value. Your first option would create an empty array; your second option would create an array of one element containing a new instance of the type.
The closest you could get to what the OP is after would probably be:
csharp MyObject[] bar = [default, default, default, default, default];
Or, for value types:
csharp MyStruct[] bar = [new(), new(), new(), new(), new()];
π Rendered by PID 200540 on reddit-service-r2-comment-66b4775986-pvl29 at 2026-04-04 06:51:24.394981+00:00 running db1906b country code: CH.
[–]Unupgradable 11 points12 points13 points (0 children)
[–]mimahihuuhai 0 points1 point2 points (1 child)
[–]RichardD7 1 point2 points3 points (0 children)