Can I do something like this in C#? (Regarding array of a class type)

Hi,

I have a class that is actually a small data structure and is some fields. I want to create an array of these class and initialize it when I’m defining it.

So my class is like this:

public class Wave
{
	public int numOfFoods;
	public int variation;
	public int RestTime;
	public int Speed;
}

and I’m interested to initialize it something like this, as I can in Lua:

Waves = {}
    		Waves[1] = {}
    		Waves[1][1]  =    {numOfFoods = 3, variation = 3, RestTime = 0.6, speed = 60, position = "center"} --60
    		Waves[1][2]  =    {numOfFoods = 3, variation = 6, RestTime = 0.6, speed = 70, position = "center"} 
    		Waves[1][3]  =    {numOfFoods = 6, variation = 3, RestTime = 0.6, speed = 60, position = "center"} --30
    		Waves[1][4]  =    {numOfFoods = 6, variation = 6, RestTime = 0.6, speed = 50, position = "center"}
    		Waves[1][5]  =    {numOfFoods = 6, variation = 9, RestTime = 0.6, speed = 60, position = "center"} --40
    	
	    	Waves[2] = {}
    		Waves[2][1]  =    {numOfFoods = 3, variation = 3, RestTime = 0.6, speed = 80 , position = "sides"}
    		Waves[2][2]  =    {numOfFoods = 3, variation = 6, RestTime = 0.6, speed = 110, position = "sides"}
    		Waves[2][3]  =    {numOfFoods = 6, variation = 3, RestTime = 0.6, speed = 90 , position = "sides"}
    		Waves[2][4]  =    {numOfFoods = 6, variation = 6, RestTime = 0.6, speed = 100, position = "sides"}
    		Waves[2][5]  =    {numOfFoods = 6, variation = 9, RestTime = 0.6, speed = 100, position = "sides"}
    		Waves[2][6]  =    {numOfFoods = 9, variation = 3, RestTime = 0.6, speed = 80 , position = "sides"}
    		Waves[2][7]  =    {numOfFoods = 9, variation = 3, RestTime = 0.6, speed = 110, position = "sides"}
    		Waves[2][8]  =    {numOfFoods = 9, variation = 3, RestTime = 0.6, speed = 90 , position = "sides"}
    		Waves[2][9]  =    {numOfFoods = 12, variation = 3, RestTime = 0.6, speed = 100, position = "sides"}
    		Waves[2][10] =    {numOfFoods = 12, variation = 6, RestTime = 0.6, speed = 110, position = "sides"}

(For not generating garbage, I’m avoiding List if possible, plus my array’s size is fixed)

I know how to do so with array of integers but not how to do the same with array of a custom class.

Thanks!

Assuming you correct variable names casing, change RestTime to correct data type e.g. float, and add position variable to your class, you can initialize your waves this way:

Wave[][] waves = 
{
    new Wave[]
    {
        new Wave {numOfFoods = 3, variation = 3, RestTime = 0.6f, Speed = 10, position = "center"},
        new Wave {numOfFoods = 3, variation = 3, RestTime = 0.6f, Speed = 20, position = "center"}
    },
    new Wave[]
    {
        new Wave {numOfFoods = 3, variation = 3, RestTime = 0.6f, Speed = 30, position = "sides"},
        new Wave {numOfFoods = 3, variation = 3, RestTime = 0.6f, Speed = 40, position = "sides"},
        new Wave {numOfFoods = 3, variation = 3, RestTime = 0.6f, Speed = 50, position = "sides"},
        new Wave {numOfFoods = 3, variation = 3, RestTime = 0.6f, Speed = 60, position = "sides"}
    }
};

and some test:

Debug.Log(waves.Length);
foreach (var w1 in waves)
{
    Debug.Log(w1.Length);
    foreach (var w2 in w1)
    {
        Debug.Log(w2.Speed);
    }
}