Quantcast
Channel: Array slices in C# - Stack Overflow
Browsing latest articles
Browse All 21 View Live

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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer by Simon Giles for Array slices in C#

For byte arrays System.Buffer.BlockCopy will give you the very best performance.

View Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer 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 Article

Answer by greyline for Array slices in C#

byte[] foo = new byte[4096]; byte[] bar = foo.Take(40).ToArray();

View Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer by Marc Gravell for Array slices in C#

If you want IEnumerable<byte>, then justIEnumerable<byte> data = foo.Take(x);

View Article

Answer 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 Article

Answer 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 Article

Array 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

Browsing latest articles
Browse All 21 View Live




Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>