C# Splitting An Array

后端 未结 9 1353
情深已故
情深已故 2020-12-10 00:39

I need to split an array of indeterminate size, at the midpoint, into two separate arrays.

The array is generated from a list of strings using ToArray().

<         


        
9条回答
  •  我在风中等你
    2020-12-10 01:36

    You could use the following method to split an array into 2 separate arrays

    public void Split(T[] array, int index, out T[] first, out T[] second) {
      first = array.Take(index).ToArray();
      second = array.Skip(index).ToArray();
    }
    
    public void SplitMidPoint(T[] array, out T[] first, out T[] second) {
      Split(array, array.Length / 2, out first, out second);
    }
    

提交回复
热议问题