Answer by Dave Glassborow for Array slices in C#
Often when reading from streams you want to handle reading less that requested.Array.Resize is your friend in this situation.
View ArticleAnswer by Major for Array slices in C#
C# 8 now (since 2019) supports Ranges which allows you to achieve Slice much easier (similar to JS syntax):var array = new int[] { 1, 2, 3, 4, 5 };var slice1 = array[2..^3]; // array[new Range(2, new...
View ArticleAnswer by Ahmad AlSaloum for Array slices in C#
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace data_seniens{ class Program { static void Main(string[] args) { //new list float...
View ArticleAnswer by Remy for Array slices in C#
Starting from C# 8.0/.Net Core 3.0Array slicing will be supported, along with the new types Index and Range being added. Range Struct docsIndex Struct docsIndex i1 = 3; // number 3 from beginningIndex...
View ArticleAnswer by Patrick Hofman for Array slices in C#
In C# 7.2, you can use Span<T>. The benefit of the new System.Memory system is that it doesn't need copying around data.The method you need is Slice:Span<byte> slice = foo.Slice(0, 40);A...
View ArticleAnswer by Simon Giles for Array slices in C#
For byte arrays System.Buffer.BlockCopy will give you the very best performance.
View ArticleAnswer by Dimitris for Array slices in C#
If you don't want to add LINQ or other extensions just do:float[] subArray = new List<float>(myArray).GetRange(0, 8).ToArray();
View ArticleAnswer by Brendan Taylor for Array slices in C#
Here is an extension function that uses a generic and behaves like the PHP function array_slice. Negative offset and length are allowed.public static class Extensions{ public static T[]...
View ArticleAnswer by Li Zhen for Array slices in C#
This may be a solution that:var result = foo.Slice(40, int.MaxValue);Then the result is an IEnumerable< IEnumerable< byte>> with a first IEnumerable< byte> contains the first 40 bytes...
View ArticleAnswer by Vladimir Mitrovic for Array slices in C#
Here's a simple extension method that returns a slice as a new array:public static T[] Slice<T>(this T[] arr, uint indexFrom, uint indexTo) { if (indexFrom > indexTo) { throw new...
View ArticleAnswer by greyline for Array slices in C#
byte[] foo = new byte[4096]; byte[] bar = foo.Take(40).ToArray();
View ArticleAnswer by Ken Smith for Array slices in C#
Another possibility I haven't seen mentioned here: Buffer.BlockCopy() is slightly faster than Array.Copy(), and it has the added benefit of being able to convert on-the-fly from an array of primitives...
View ArticleAnswer by Mike Scott for Array slices in C#
You could use ArraySegment<T>. It's very light-weight as it doesn't copy the array:string[] a = { "one", "two", "three", "four", "five" };var segment = new ArraySegment<string>( a, 1, 2 );
View ArticleAnswer by WOPR for Array slices in C#
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);
View ArticleAnswer by Rauhotz for Array slices in C#
You could use a wrapper around the original array (which is IList), like in this (untested) piece of code.public class SubList<T> : IList<T>{ #region Fields private readonly int startIndex;...
View ArticleAnswer by peSHIr for Array slices in C#
Arrays are enumerable, so your foo already is an IEnumerable<byte> itself.Simply use LINQ sequence methods like Take() to get what you want out of it (don't forget to include the Linq namespace...
View ArticleAnswer by aku for Array slices in C#
You can use Take extension method var array = new byte[] {1, 2, 3, 4};var firstTwoItems = array.Take(2);
View ArticleAnswer by Marc Gravell for Array slices in C#
If you want IEnumerable<byte>, then justIEnumerable<byte> data = foo.Take(x);
View ArticleAnswer by bleevo for Array slices in C#
I do not think C# supports the Range semantics. You could write an extension method though, like:public static IEnumerator<Byte> Range(this byte[] array, int start, int end);But like others have...
View ArticleAnswer by Arjan Einbu for Array slices in C#
You could use the arrays CopyTo() method.Or with LINQ you can use Skip() and Take()...byte[] arr = {1, 2, 3, 4, 5, 6, 7, 8};var subset = arr.Skip(2).Take(2);
View ArticleArray slices in C#
How do you do it? Given a byte array:byte[] foo = new byte[4096];How would I get the first x bytes of the array as a separate array? (Specifically, I need it as an IEnumerable<byte>)This is for...
View Article
More Pages to Explore .....