C# quickest way to shift array

前端 未结 20 1321
礼貌的吻别
礼貌的吻别 2020-12-01 01:35

How can I quickly shift all the items in an array one to the left, padding the end with null?

For example, [0,1,2,3,4,5,6] would become [1,2,3,4,5,6,null]

Ed

20条回答
  •  感情败类
    2020-12-01 02:28

    Use the Array.Copy() method as in

    int?[] myArray = new int?[]{0,1,2,3,4};
    Array.Copy(myArray, 1, myArray, 0, myArray.Length - 1);
    myArray[myArray.Length - 1] = null
    

    The Array.Copy is probably the way, Microsoft wanted us to copy array elements...

提交回复
热议问题