title: ArraySegment - Returning the actual segment C#
body: <p>I have been looking around on ways to return the segment which is basically held by ArraySegment in terms of offset and count. Although ArraySegment holds the complete and original array, it just delimits it with the fact that any changes to the segment are reflected into the original. The problem or say the limitation with ArraySegment is that it will not return the segment itself as a whole and I have to traverse the values. What would be the best way to return the segment as a whole? </p>
  
  <pre><code> byte[] input = new byte[5]{1,2,3,4,5};
   ArraySegment&lt;byte&gt; delimited = new ArraySegment&lt;byte&gt;(input,0,2);
   byte[] segment = HERE I NEED SOMETHING THAT WILL RETURN THE SEGMENT i.e. [0,1,2]
  </code></pre>
  
  <p>The most important point, segment must not be a copy but should refer to the original array. If any changes to segment are done, they must be reflected in the original array.</p>
  
  <p>Any tips are highly appreciated, thanks!</p>
  
  <p><strong>ASSIGNMENT BENCHMARKS</strong> : After some answers from <a href="https://stackoverflow.com/users/98713/thomas-levesque">Thomas</a> and <a href="https://stackoverflow.com/users/316644/digemall">digEmAll</a></p>
  
  <p>Ok, I ran some benchmarks against the code from digEmAll and Thomas, and to my surprise the code is overwhelmingly faster. Just what I was desperately looking for. Here are the results.</p>
  
  <pre><code>Construct             Size    Elements assigned    Iterations       Time
  _______________________________________________________________________________
  
  ArraySegmentWrapper   1500        1500              1000000       396.3 ms
  Array.Copy            1500        1500              1000000       4389.04 ms
  </code></pre>
  
  <p>As you can see the whopping difference, it is very clear to me that I shall be using the code for ArraySegment. Below is the benchmarking code. Please note that this might be a bit
  biased as people would argue why "new" has been put inside a loop. I am just trying to reproduce the situation I currently have at hand at resolve it as much as possible without moving much of the code. This just made my day!</p>
  
  <pre><code>namespace ArraySegmentWrapped
  {
      class Program
      {
  
          public static Stopwatch stopWatch = new Stopwatch();
          public static TimeSpan span = new TimeSpan();
          public static double totalTime = 0.0;
          public static int iterations = 1000000;
  
          static void Main(string[] args)
          {
              int size = 1500;
              int startIndex = 0;
              int endIndex = 1499;
              byte[] array1 = new byte[size];
              byte[] array2 = null;
  
              for (int index = startIndex; index &lt; size; index++)
              {
                  array1[index] = (byte)index;
              }
  
              ArraySegmentWrapper&lt;byte&gt; arraySeg;
  
              for (int index = 0; index &lt; iterations; index++)
              {
                  stopWatch.Start();
                  arraySeg = new ArraySegmentWrapper&lt;byte&gt;(array1, startIndex, endIndex);            
                  stopWatch.Stop();
                  totalTime += stopWatch.Elapsed.TotalMilliseconds;
              }
  
              Console.WriteLine("ArraySegment:{0:F6}", totalTime / iterations);
              stopWatch.Reset();
              totalTime = 0.0;
  
              for (int index = 0; index &lt; iterations; index++)
              {
                  stopWatch.Start();
                  array2 = new byte[endIndex - startIndex + 1];
                  Array.Copy(array1, startIndex, array2, 0, endIndex);
                  stopWatch.Stop();
                  totalTime += stopWatch.Elapsed.TotalMilliseconds;
              }
              Console.WriteLine("Array.Copy:{0:F6}", totalTime / iterations);                        
  
  
          }
      }
  // Code for ArraySegmentWrapper goes here    
  
  }
  </code></pre>
  
  <p><strong>ACCESS BENCHMARKS (Updated)</strong> 
   So after what <a href="https://stackoverflow.com/users/98713/thomas-levesque">Thomas</a> had pointed about the benchmarks and said that access to simple arrays would be faster as compared to ArraySegment, he was totally right. But with  digEmAll pointing out that I should test in Release mode (sorry for the old mistake of testing in debug mode), I left the code almost the same as above(iterations reduced by two zeros - could not wait very long for the output to come, sorry) and some modifications to access the same number of elements, Below is what I got.</p>
  
  <pre><code>Construct             Size    Elements accessed    Iterations       Time
  _______________________________________________________________________________
  
  ArraySegmentWrapper   1500        1500              1000000       5268.3 ms
  Array.Copy            1500        1500              1000000       4812.4 ms
  </code></pre>
  
  <p>Concluded that although assingment is very fast, access is slow via ArraySegments.</p>