This post will discuss how to split an array into two equal-size parts in C#. If the number of elements is odd, the first half should accommodate the extra element.

1. Using Linq

The Enumerable.Take() method returns a supplied number of elements from the start of a sequence and the Enumerable.Skip() method skips the supplied number of items in a sequence. It can be used as follows to split an array into two equal-size parts:

Download  Run Code

2. Using Array.Copy method

The Array.Copy method copies a range of elements from an array to another array. It can be used as follows to copy the first half and the second half from the original array to new arrays:

Download  Run Code

3. Using Range Notation

Since C# 8, you can use the range notation .., which specifies the start and end of a range as its operands. The start of the range is inclusive and the end of the range is exclusive. The following code example shows splits an array using ranges that are open-ended at the start and the end:

Download  Run Code

That’s all about splitting an array into two equal-size parts in C#.