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