Explanation for array initialization peculiarities
I'd like to get an explanation for the following behavior :
Why does
{}
seem to do something different depending on where it is ?56 Replies
It shouldn't behave differently
And yet, it does haha
Just to check, though, could you try
[]
instead of {}
?in which one ?
Of = []
this is not proper syntax it seems
What's your .NET version?
this works fine btw
This intializes a new array of 0-length explicitly, yes
is this what you're looking for ?
Yeah
I guess the
[]
syntax is 8.0 and upBut why does
{}
initialize a 0 length array in some cases and not in others ?¯\_(ツ)_/¯
Should I upgrade to that then ?
What's the reason for this syntax not working ?
This I meant
Honestly not sure
Just tried it on .NET 8 and it doesn't work either
Do you think it could be a bug in the compiler ?
E.g.
Not sure
I'm looking for some sort of specification that says "When classes are initialized this way {} stands for null"
or something like that
Huh, the compiler-generated code is
It does not assign a new array to
Of
It works if I do Of = []
though
generates
So the easiest solution would be upgrade to .NET 8 and use []
Far as explanation goes, no ideaNow I have to look for this little thing everywhere in my codebase I used it a bunch x)
Do you know anyone who might have one ?
I'm just curious
Huh, there are some MS people from the language and compiler team on the server
I think it might be related to this syntax
Might be
{}
can, contextually, be an object initializer not an array initializer
While []
is always a collection initializerDo they have a specific role / is there someone who is open to be pinged ?
No such role, alas
I thought it should be preceded by "new" if it's an object initializer
I'll post it in the associates channel, though
and it's weird that it just gives null
Alright thank you!
Thank you for your time as well
there seems to be a verified microsoft employee role but I think it might be best not to ping them directly
@mtreit?
:when:
Oh well x)
Care to weigh in?
What are we discussing?
Haven't scrolled up.
reason those two
are different
Good question, I don't think I've ever used that syntax.
Thing = {}
in an object initializer is the syntax for adding things, not creating a collection
for exampleAaron
REPL Result: Failure
Exception: NullReferenceException
Quoted by
<@270457259065081856> from #taxed-void (click here)
Compile: 283.852ms | Execution: 60.462ms | React with ❌ to remove this embed.
the list is null, it tries to do List.Add, and throws
It works on arrays though
thats not in an object initializer though
thats where the difference exists
the syntax means something different in a difference place
Ah, I figured it was something like that
if you want to create the collection, use
[]
instead of {}
Well, apparently that's in dotnet 8.0, I haven't upgraded to that yet
I'm using Array.Empty now
Just switch the number
Upgrade to 8.0 should literally be just it
that is also fine
No other changes required
don't I need to install it though
And that is not a problem either
besides, yknow, the list of changes required https://learn.microsoft.com/en-us/dotnet/core/compatibility/8.0
Breaking changes in .NET 8
Navigate to the breaking changes in .NET 8.
Those are few and mostly to obscure APIS, IME
winget install --id Microsoft.DotNet.SDK.8
Alright alright fine I'll do it now x)
Alternatively, if you use VS, chances are the SDK should be installed with it
dotnet --list-sdks
will answer that questionI use Rider
It didn't seem to be installed yet
Ah
Then yeah, install it with Winget
Well, thank you all for your help
Much appreciated !