C# array init

if I initialize an array of structs, which constructor does it use if any?
MyStruct[,] array = new MyStruct[2, 3];

struct MyStruct {
public MyStruct() {
// ctor
}

public MyStruct(int x) {
// another ctor
}
}
MyStruct[,] array = new MyStruct[2, 3];

struct MyStruct {
public MyStruct() {
// ctor
}

public MyStruct(int x) {
// another ctor
}
}
57 Replies
MODiX
MODiX2mo ago
Angius
REPL Result: Success
MyStruct[,] array = new MyStruct[2, 3];

struct MyStruct {
public MyStruct() {
Console.WriteLine("one");
}

public MyStruct(int x) {
Console.WriteLine("two");
}
}
MyStruct[,] array = new MyStruct[2, 3];

struct MyStruct {
public MyStruct() {
Console.WriteLine("one");
}

public MyStruct(int x) {
Console.WriteLine("two");
}
}
Compile: 464.482ms | Execution: 35.715ms | React with ❌ to remove this embed.
Angius
Angius2mo ago
Neither
שלום אני בן עמי
can I make it use any of them?
Angius
Angius2mo ago
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[]
Angius
Angius2mo ago
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
Angius
Angius2mo ago
I guess C# just uses some other, magic constructor
Servator
Servator2mo ago
public string this[int index] { }
public string this[int index] { }
Are you looking for this?
Angius
Angius2mo ago
And fills the properties of the struct with default values
Angius
Angius2mo ago
No description
Angius
Angius2mo ago
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
Jimmacle
Jimmacle2mo ago
the default for value types is zeroed out, not undefined same way that the default for reference types is null (0)
230V
230V2mo ago
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
Jimmacle
Jimmacle2mo ago
Enumerable.Repeat(new MyStruct(), 100).ToArray() :when:
230V
230V2mo ago
Something like Array.Fill(array, new MyStruct()); too I think
Jimmacle
Jimmacle2mo ago
yeah, if you already have the array
שלום אני בן עמי
both of those dont work on 2d arrays ✨ aint C# great
Jimmacle
Jimmacle2mo ago
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
Jimmacle
Jimmacle2mo ago
3 lines
שלום אני בן עמי
i've seen how Func is defined still isn't great
Jimmacle
Jimmacle2mo ago
Func is just a delegate, there isn't really anything to it just a bunch of variations to account for different numbers of arguments
שלום אני בן עמי
still looks like this though:
Func<TResult>
Func<T,TResult>
Func<T1,T2,TResult>
Func<T1,T2,T3,TResult>
Func<T1,T2,T3,T4,TResult>
Func<T1,T2,T3,T4,T5,TResult>
Func<T1,T2,T3,T4,T5,T6,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,TResult>
Func<TResult>
Func<T,TResult>
Func<T1,T2,TResult>
Func<T1,T2,T3,TResult>
Func<T1,T2,T3,T4,TResult>
Func<T1,T2,T3,T4,T5,TResult>
Func<T1,T2,T3,T4,T5,T6,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,TResult>
Func<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,TResult>
Jimmacle
Jimmacle2mo ago
and? variadic generic arguments aren't a thing
שלום אני בן עמי
I know, but this still sucks
Jimmacle
Jimmacle2mo ago
¯\_(ツ)_/¯
שלום אני בן עמי
it's like waiting for a bus when you have no roof and it rains you can't evade the situation but it sucks
Jimmacle
Jimmacle2mo ago
i don't think about this at all personally it doesn't affect anything i do
Angius
Angius2mo ago
I really have no idea why it bothers you tbh
230V
230V2mo ago
//shit happens
Angius
Angius2mo ago
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
Jimmacle
Jimmacle2mo ago
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?
Jimmacle
Jimmacle2mo ago
then write your own delegate
Angius
Angius2mo ago
^
Jimmacle
Jimmacle2mo ago
if you have a 17 argument function that's probably an issue in itself anyway
Petris
Petris2mo ago
Array.Initialize Method (System)
Initializes every element of the value-type Array by calling the parameterless constructor of the value type.
Jimmacle
Jimmacle2mo ago
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
Angius
Angius2mo ago
Yeah, I'd imagine at this point you would really want all of those params named
שלום אני בן עמי
thank you
Petris
Petris2mo ago
just note that it'll be slower than a simple loop
Petris
Petris2mo ago
or actually, not sure about a loop on an MD one
שלום אני בן עמי
why would it be though?
שלום אני בן עמי
but it's not a lot more than just a loop
Jimmacle
Jimmacle2mo ago
you asked why it would be slower and that's why
שלום אני בן עמי
but it's like 3 if checks
Angius
Angius2mo ago
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
Petris
Petris2mo ago
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
Jimmacle
Jimmacle2mo ago
you could just benchmark the options