C
C#8mo ago
המשורר

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
MODiX8mo 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
Angius8mo ago
Neither
המשורר
המשוררOP8mo ago
can I make it use any of them?
Angius
Angius8mo ago
What exactly do you want to achieve?
המשורר
המשוררOP8mo ago
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
Angius8mo ago
Well, yeah But, what, you want the array to be pre-filled with structs and those structs with default values?
המשורר
המשוררOP8mo ago
yeah, i figured creating a default constructor would do but ig C# never fails to confuse me
Angius
Angius8mo ago
I guess C# just uses some other, magic constructor
Servator
Servator8mo ago
public string this[int index] { }
public string this[int index] { }
Are you looking for this?
Angius
Angius8mo ago
And fills the properties of the struct with default values
Angius
Angius8mo ago
No description
Angius
Angius8mo 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
Jimmacle8mo ago
the default for value types is zeroed out, not undefined same way that the default for reference types is null (0)
230V
230V8mo ago
A loop, an initializer or any other way of initializing elements used later is required here
המשורר
המשוררOP8mo ago
well that seems dumb imo maybe im dumb and I can't comprehend it
Jimmacle
Jimmacle8mo ago
Enumerable.Repeat(new MyStruct(), 100).ToArray() :when:
230V
230V8mo ago
Something like Array.Fill(array, new MyStruct()); too I think
Jimmacle
Jimmacle8mo ago
yeah, if you already have the array
המשורר
המשוררOP8mo ago
both of those dont work on 2d arrays ✨ aint C# great
Jimmacle
Jimmacle8mo ago
2d arrays aren't common anyway in my experience but if you need them, just write a loop
המשורר
המשוררOP8mo ago
doesn't really matter to be fair
Jimmacle
Jimmacle8mo ago
3 lines
המשורר
המשוררOP8mo ago
i've seen how Func is defined still isn't great
Jimmacle
Jimmacle8mo 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
המשורר
המשוררOP8mo ago
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
Jimmacle8mo ago
and? variadic generic arguments aren't a thing
המשורר
המשוררOP8mo ago
I know, but this still sucks
Jimmacle
Jimmacle8mo ago
¯\_(ツ)_/¯
המשורר
המשוררOP8mo 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
Jimmacle8mo ago
i don't think about this at all personally it doesn't affect anything i do
Angius
Angius8mo ago
I really have no idea why it bothers you tbh
230V
230V8mo ago
//shit happens
Angius
Angius8mo ago
The lack of Array.Fill for n-dim arrays, sure, I understand But how Func types are defined...?
המשורר
המשוררOP8mo ago
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
Jimmacle8mo ago
i mean, when's the last time the way func was defined interfered with you using C# to solve a problem
המשורר
המשוררOP8mo ago
what about my 17-argument function?
Jimmacle
Jimmacle8mo ago
then write your own delegate
Angius
Angius8mo ago
^
Jimmacle
Jimmacle8mo ago
if you have a 17 argument function that's probably an issue in itself anyway
Petris
Petris8mo ago
Array.Initialize Method (System)
Initializes every element of the value-type Array by calling the parameterless constructor of the value type.
Jimmacle
Jimmacle8mo 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
Angius8mo ago
Yeah, I'd imagine at this point you would really want all of those params named
המשורר
המשוררOP8mo ago
thank you
Petris
Petris8mo ago
just note that it'll be slower than a simple loop
המשורר
המשוררOP8mo ago
why
Petris
Petris8mo ago
or actually, not sure about a loop on an MD one
המשורר
המשוררOP8mo ago
why would it be though?
המשורר
המשוררOP8mo ago
but it's not a lot more than just a loop
Jimmacle
Jimmacle8mo ago
you asked why it would be slower and that's why
המשורר
המשוררOP8mo ago
but it's like 3 if checks
Angius
Angius8mo ago
All three of which cost some performance Not much, but some So, yes, it would be marginally slower than a simple loop
המשורר
המשוררOP8mo ago
for my not performence sensitive project it will suffice thanks though
Petris
Petris8mo ago
there's also the fact that it calls the constructor through a pointer there which will be slower
המשורר
המשוררOP8mo ago
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
Jimmacle8mo ago
you could just benchmark the options
המשורר
המשוררOP8mo ago
i might
Want results from more Discord servers?
Add your server