<p>I have a byte array in memory, read from a file.  I would like to split the byte array at a certain point (index) without having to just create a new byte array and copy each byte at a time, increasing the in memory foot print of the operation. What I would like is something like this:</p>

<pre><code>byte[] largeBytes = [1,2,3,4,5,6,7,8,9];  
byte[] smallPortion;  
smallPortion = split(largeBytes, 3);  
</code></pre>

<p><code>smallPortion</code> would equal 1,2,3,4<br>
<code>largeBytes</code> would equal 5,6,7,8,9</p>

[{'answer_id': 20826, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 0, 'selected': False, 'text': "<p>You can't. What you might want is keep a starting point and number of items; in essence, build iterators. If this is C++, you can just use <code>std::vector&lt;int&gt;</code> and use the built-in ones.</p>\n\n<p>In C#, I'd build a small iterator class that holds start index, count and implements <code>IEnumerable&lt;&gt;</code>.</p>\n"}, {'answer_id': 20949, 'author': 'Michał Piaskowski', 'author_id': 1534, 'author_profile': 'https://Stackoverflow.com/users/1534', 'pm_score': 5, 'selected': True, 'text': '<p>This is how I would do that:</p>\n\n<pre><code>using System;\nusing System.Collections;\nusing System.Collections.Generic;\n\nclass ArrayView&lt;T&gt; : IEnumerable&lt;T&gt;\n{\n    private readonly T[] array;\n    private readonly int offset, count;\n\n    public ArrayView(T[] array, int offset, int count)\n    {\n        this.array = array;\n        this.offset = offset;\n        this.count = count;\n    }\n\n    public int Length\n    {\n        get { return count; }\n    }\n\n    public T this[int index]\n    {\n        get\n        {\n            if (index &lt; 0 || index &gt;= this.count)\n                throw new IndexOutOfRangeException();\n            else\n                return this.array[offset + index];\n        }\n        set\n        {\n            if (index &lt; 0 || index &gt;= this.count)\n                throw new IndexOutOfRangeException();\n            else\n                this.array[offset + index] = value;\n        }\n    }\n\n    public IEnumerator&lt;T&gt; GetEnumerator()\n    {\n        for (int i = offset; i &lt; offset + count; i++)\n            yield return array[i];\n    }\n\n    IEnumerator IEnumerable.GetEnumerator()\n    {\n        IEnumerator&lt;T&gt; enumerator = this.GetEnumerator();\n        while (enumerator.MoveNext())\n        {\n            yield return enumerator.Current;\n        }\n    }\n}\n\nclass Program\n{\n    static void Main(string[] args)\n    {\n        byte[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };\n        ArrayView&lt;byte&gt; p1 = new ArrayView&lt;byte&gt;(arr, 0, 5);\n        ArrayView&lt;byte&gt; p2 = new ArrayView&lt;byte&gt;(arr, 5, 5);\n        Console.WriteLine("First array:");\n        foreach (byte b in p1)\n        {\n            Console.Write(b);\n        }\n        Console.Write("\\n");\n        Console.WriteLine("Second array:");\n        foreach (byte b in p2)\n        {\n            Console.Write(b);\n        }\n        Console.ReadKey();\n    }\n}\n</code></pre>\n'}, {'answer_id': 20990, 'author': 'Tim Frey', 'author_id': 1471, 'author_profile': 'https://Stackoverflow.com/users/1471', 'pm_score': 1, 'selected': False, 'text': "<p>I'm not sure what you mean by:</p>\n\n<blockquote>\n  <p>I would like to split the byte array at a certain point(index) without having to just create a new byte array and copy each byte at a time, increasing the in memory foot print of the operation.</p>\n</blockquote>\n\n<p>In most languages, certainly C#, once an array has been allocated, there is no way to change the size of it.  It sounds like you're looking for a way to change the length of an array, which you can't.  You also want to somehow recycle the memory for the second part of the array, to create a second array, which you also can't do.</p>\n\n<p>In summary: just create a new array.</p>\n"}, {'answer_id': 1662438, 'author': 'Eren Ersönmez', 'author_id': 201088, 'author_profile': 'https://Stackoverflow.com/users/201088', 'pm_score': 5, 'selected': False, 'text': "<p>FYI.  <code>System.ArraySegment&lt;T&gt;</code> structure basically is the same thing as <code>ArrayView&lt;T&gt;</code> in the code above.  You can use this out-of-the-box structure in the same way, if you'd like.  </p>\n"}, {'answer_id': 5057108, 'author': 'Alireza Naghizadeh', 'author_id': 625217, 'author_profile': 'https://Stackoverflow.com/users/625217', 'pm_score': 5, 'selected': False, 'text': '<p>In C# with Linq you can do this:</p>\n\n<pre><code>smallPortion = largeBytes.Take(4).ToArray();\nlargeBytes = largeBytes.Skip(4).Take(5).ToArray();\n</code></pre>\n\n<p>;)</p>\n'}, {'answer_id': 13176553, 'author': 'Robert Wisniewski', 'author_id': 1791254, 'author_profile': 'https://Stackoverflow.com/users/1791254', 'pm_score': 2, 'selected': False, 'text': '<p>Try this one: </p>\n\n<pre><code>private IEnumerable&lt;byte[]&gt; ArraySplit(byte[] bArray, int intBufforLengt)\n    {\n        int bArrayLenght = bArray.Length;\n        byte[] bReturn = null;\n\n        int i = 0;\n        for (; bArrayLenght &gt; (i + 1) * intBufforLengt; i++)\n        {\n            bReturn = new byte[intBufforLengt];\n            Array.Copy(bArray, i * intBufforLengt, bReturn, 0, intBufforLengt);\n            yield return bReturn;\n        }\n\n        int intBufforLeft = bArrayLenght - i * intBufforLengt;\n        if (intBufforLeft &gt; 0)\n        {\n            bReturn = new byte[intBufforLeft];\n            Array.Copy(bArray, i * intBufforLengt, bReturn, 0, intBufforLeft);\n            yield return bReturn;\n        }\n    }\n</code></pre>\n'}, {'answer_id': 44464131, 'author': 'orad', 'author_id': 450913, 'author_profile': 'https://Stackoverflow.com/users/450913', 'pm_score': 2, 'selected': False, 'text': '<p>As <a href="https://stackoverflow.com/a/1662438/450913">Eren said</a>, you can use <code>ArraySegment&lt;T&gt;</code>. Here\'s an extension method and usage example:</p>\n\n<pre><code>public static class ArrayExtensionMethods\n{\n    public static ArraySegment&lt;T&gt; GetSegment&lt;T&gt;(this T[] arr, int offset, int? count = null)\n    {\n        if (count == null) { count = arr.Length - offset; }\n        return new ArraySegment&lt;T&gt;(arr, offset, count.Value);\n    }\n}\n\nvoid Main()\n{\n    byte[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };\n    var p1 = arr.GetSegment(0, 5);\n    var p2 = arr.GetSegment(5);\n    Console.WriteLine("First array:");\n    foreach (byte b in p1)\n    {\n        Console.Write(b);\n    }\n    Console.Write("\\n");\n    Console.WriteLine("Second array:");\n    foreach (byte b in p2)\n    {\n        Console.Write(b);\n    }\n}\n</code></pre>\n'}, {'answer_id': 66403734, 'author': 'Yefka', 'author_id': 1008954, 'author_profile': 'https://Stackoverflow.com/users/1008954', 'pm_score': 0, 'selected': False, 'text': '<p>I tried different algorithms :</p>\n<ul>\n<li>Skip().Take() =&gt; the worst, by far</li>\n<li>Array.Copy</li>\n<li>ArraySegment</li>\n<li>new Guid(int, int16, int16 ...)</li>\n</ul>\n<p>The latest being the fastest I\'m now using this extension method:</p>\n<pre class="lang-cs prettyprint-override"><code>        public static Guid ToGuid(this byte[] byteArray, int offset)\n        {\n            return new Guid(BitConverter.ToInt32(byteArray, offset), BitConverter.ToInt16(byteArray, offset + 4), BitConverter.ToInt16(byteArray, offset + 6), byteArray[offset + 8], byteArray[offset + 9], byteArray[offset + 10], byteArray[offset + 11], byteArray[offset + 12], byteArray[offset + 13], byteArray[offset + 14], byteArray[offset + 15]);\n        }\n</code></pre>\n<p>With a byte array with 10000000 guids:</p>\n<pre><code>Done (Skip().Take()) in 1,156ms (for only 100000 guids :))\nDone (Array.Copy) in 1,219ms\nDone (ToGuid extension) in 994ms\nDone (ArraySegment) in 2,411ms\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/20797', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1048/']