Slices of an array in C#

Problem

How do you go about doing it? Given a byte array, perform the following:

``````byte[] foo = new byte[4096];
``````

What’s the best way to get the first x bytes of an array as a separate array? (I need it as an IEnumerablebyte> in particular.)

This is where you’ll work with sockets. The simplest method, I believe, is array slicing, which is comparable to Perl syntax:

``````@bar = @foo[0..40];
``````

The first 41 entries of the @bar array would be returned. Is there something in C# that I’m overlooking, or should I be doing something else?

If that helps, LINQ is an option for me (.NET 3.5).

Solution #1

ArraySegmentT> could be used. It’s really light because it doesn’t duplicate the array:

``````string[] a = { "one", "two", "three", "four", "five" };
var segment = new ArraySegment<string>( a, 1, 2 );
``````

Solution #2

Because arrays are enumerable, your foo is already an IEnumerablebyte>. To obtain what you want out of it, simply utilize LINQ sequence methods like Take() (don’t forget to specify the Linq namespace when using System.Linq;):

``````byte[] foo = new byte[4096];

var bar = foo.Take(41);
``````

You may use the ToArray() method to create an array from any IEnumerablebyte> value if you really need one. That does not appear to be the situation in this instance.

Solution #3

You might use the CopyTo() method on arrays.

You may also use Skip() and Take() in LINQ ()…

``````byte[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
var subset = arr.Skip(2).Take(2);
``````

Solution #4

Beginning with C# 8.0/.Net Core 3.0,

Array slicing will be enabled, as well as the addition of the new types Index and Range.

Documents on Range Structuring Struct docs index

``````Index i1 = 3;  // number 3 from beginning
Index i2 = ^4; // number 4 from end
int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine(\$"{a[i1]}, {a[i2]}"); // "3, 6"

var slice = a[i1..i2]; // { 3, 4, 5 }
``````

The code in the example above was obtained from the C# 8.0 blog.

It’s worth noting that the prefix denotes counting from the beginning of the array. As shown in the example in the documentation

``````var words = new string[]
{
// index from start    index from end
"The",      // 0                   ^9
"quick",    // 1                   ^8
"brown",    // 2                   ^7
"fox",      // 3                   ^6
"jumped",   // 4                   ^5
"over",     // 5                   ^4
"the",      // 6                   ^3
"lazy",     // 7                   ^2
"dog"       // 8                   ^1
};              // 9 (or words.Length) ^0
``````

Range and Index are useful in situations other than slicing arrays, such as loops.

``````Range range = 1..4;
foreach (var name in names[range])
``````

Will go through entries 1 through 4 in a loop.

Note that C# 8.0 is not yet officially released at the time of writing this answer. .NET Framework 8.x and.NET Framework.NET Framework.NET Framework.NET Framework.NET Net Core 3.x are now available in Visual Studio 2019 and onwards

Solution #5

``````static byte[] SliceMe(byte[] source, int length)
{
byte[] destfoo = new byte[length];
Array.Copy(source, 0, destfoo, 0, length);
return destfoo;
}
``````

//

``````var myslice = SliceMe(sourcearray,41);
``````