如何创建多元素对象的数组?

时间:2017-09-11 16:28:32

标签: c# arrays object

我想制作一个Instruction数组,一个需要int和两个Vector3的对象。我知道如何制作数组,但是如果可能的话,我不确定如何创建多元素对象的数组。即。

int[] myInts = new int[] {1, 4, 1, 5}; //Creates an array of ints

BUT

Instruction[] instructions = new Instruction[] { 
    {1, new Vector3(1, 5, 2), new Vector3(4, 1, 7)} 
    {2, new Vector3(6, 2, 7), new Vector3(9, 7, 4)}
}

你是这样做的,还是不同的?

1 个答案:

答案 0 :(得分:3)

您必须指定new关键字并输入:

Instruction[] instructions = new Instruction[] { 
    new Instruction {1, new Vector3(1, 5, 2), new Vector3(4, 1, 7)}, //And add comma
    new Instruction {2, new Vector3(6, 2, 7), new Vector3(9, 7, 4)}
};

查看MSDN Collection Initializers

相关问题