Arrays within array c#

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 9BitStrider
var l_stick = [["Left Stick Y", -1.0], ["Left Stick Y", 1.0], ["Left Stick X", -1.0], ["Left Stick X", 1.0]]

Just trying to find the C# equivalent to the above. Wanted to nest a few arrays within and array. Thanks.

:bust_in_silhouette: Reply From: Zylann

I dont use C# in Godot, but from what I remember you could do that:

var l_stick = new Array();
l_stick.Resize(4);

var item0 = new Array();
item0.Resize(2);
item0[0] = "Left Stick Y";
item0[1] = -1.0;
l_stick[0] = item0;

var item1 = new Array();
item1.Resize(2);
item1[0] = "Left Stick Y";
item1[1] = 1.0;
l_stick[1] = item1;

var item2 = new Array();
item2.Resize(2);
item2[0] = "Left Stick X";
item2[1] = -1.0;
l_stick[2] = item2;

var item3 = new Array();
item3.Resize(2);
item3[0] = "Left Stick X";
item3[1] = 1.0;
l_stick[3] = item3;

There might be a shorter way to write it but I don’t know if that exists for Godot arrays.

Maybe you could also try this, but I’m not sure if Godot APIs taking an Array can accept it:

object[] l_stick = new []
{
    new object[]{"Left Stick Y", -1.0},
    new object[]{"Left Stick Y", 1.0},
    new object[]{"Left Stick X", -1.0},
    new object[]{"Left Stick X", 1.0},
};

Note, if you actually dont need to pass this to Godot (like a function requiring it), and it’s only your own stuff, you don’t even have to bother with Array or even untyped object arrays, and use an array of structs instead, which is strongly typed (so you get auto-completion and checks at compile time instead of runtime):

KeyValuePair<string, float>[] l_stick = new []
{
    new KeyValuePair<string, float>("Left Stick Y", -1f),
    new KeyValuePair<string, float>("Left Stick Y", 1f),
    new KeyValuePair<string, float>("Left Stick X", -1f),
    new KeyValuePair<string, float>("Left Stick X", 1f)
};

You could also use a dictionary of string to float because your array contains pairs, pretty much. But it really depends which code needs this data.