C# quickest way to shift array

前端 未结 20 1315
礼貌的吻别
礼貌的吻别 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:30

    You can use the same array as source and destination for fast in-place copy:

    static void Main(string[] args)
            {
                int[] array = {0, 1, 2, 3, 4, 5, 6, 7};
                Array.ConstrainedCopy(array, 1, array, 0, array.Length - 1);
                array[array.Length - 1] = 0;
            }
    

提交回复
热议问题