C# array init
if I initialize an array of structs, which constructor does it use if any?
57 Replies
Angius
REPL Result: Success
Compile: 464.482ms | Execution: 35.715ms | React with ❌ to remove this embed.
Neither
can I make it use any of them?
What exactly do you want to achieve?
perhaps passing a generating function that will run for each item in the array
not having to write a
for
loop under the new MyStruct[]
Well, yeah
But, what, you want the array to be pre-filled with structs and those structs with default values?
yeah, i figured creating a default constructor would do
but ig C# never fails to confuse me
I guess C# just uses some other, magic constructor
Are you looking for this?
And fills the properties of the struct with default values
Not even assigning a default value to a property works
So just treat all the entries in the array as undefined, in whatever way a struct can be undefined
the default for value types is zeroed out, not undefined
same way that the default for reference types is null (0)
A loop, an initializer or any other way of initializing elements used later is required here
well that seems dumb imo
maybe im dumb and I can't comprehend it
Enumerable.Repeat(new MyStruct(), 100).ToArray()
:when:Something like Array.Fill(array, new MyStruct()); too I think
yeah, if you already have the array
both of those dont work on 2d arrays
✨
aint C# great
2d arrays aren't common anyway in my experience
but if you need them, just write a loop
doesn't really matter to be fair
3 lines
i've seen how
Func
is defined
still isn't greatFunc
is just a delegate, there isn't really anything to it
just a bunch of variations to account for different numbers of argumentsstill looks like this though:
and?
variadic generic arguments aren't a thing
I know, but this still sucks
¯\_(ツ)_/¯
it's like waiting for a bus when you have no roof and it rains
you can't evade the situation
but it sucks
i don't think about this at all personally
it doesn't affect anything i do
I really have no idea why it bothers you tbh
//shit happens
The lack of
Array.Fill
for n-dim arrays, sure, I understand
But how Func
types are defined...?well, it doesn't make me feel better about C# tbh
I usually write python, functional languages and sometimes C++. Now I gotta use C# and it feels weird
i gotta have something to cope about
i mean, when's the last time the way func was defined interfered with you using C# to solve a problem
what about my 17-argument function?
then write your own delegate
^
if you have a 17 argument function that's probably an issue in itself anyway
Array.Initialize Method (System)
Initializes every element of the value-type Array by calling the parameterless constructor of the value type.
16 is more than enough for any reasonable use case
i've probably never needed more than 3 or 4
at that point you should write your own delegate just for self-documenting purposes
Yeah, I'd imagine at this point you would really want all of those params named
thank you
just note that it'll be slower than a simple loop
why
or actually, not sure about a loop on an MD one
why would it be though?
https://source.dot.net/#System.Private.CoreLib/src/System/Array.CoreCLR.cs,628
because it's more than just a loop
but it's not a lot more than just a loop
you asked why it would be slower and that's why
but it's like 3 if checks
All three of which cost some performance
Not much, but some
So, yes, it would be marginally slower than a simple loop
for my not performence sensitive project it will suffice
thanks though
there's also the fact that it calls the constructor through a pointer there
which will be slower
how does it call the function otherwise?
the compiler still gonna calculate the offset for the vtable
and use it to call the method isnt it?
ig not the vtable cause no dynamic dispatch is needed but the method table of the struct
you could just benchmark the options
i might