Outputs a sub-array - CSharp System

CSharp examples for System:Array Element

Description

Outputs a sub-array

Demo Code

//  Licensed under the Apache License, Version 2.0 (the "License");
using System.Collections;
using System.Text;
using System;// www. jav  a2 s  .  c  o m

public class Main{
    /// <summary>
      /// Outputs a sub-array
      /// </summary>
      /// <param name="offset">Start index of sub-array</param>
      /// <param name="length">Length of sub-array</param>
      /// <returns>Byte array extracted from a larger byte array. Invalid paramaters result in a return of an empty byte-array.</returns>
      public static byte[] SubArray(this byte[] array, int startIndex, int length)
      {
         if (array == null || array.Length < (startIndex + length))
            return new byte[0];

         byte[] bytes = new byte[length];
         Array.Copy(array, startIndex, bytes, 0, length);
         return bytes;
      }
}

Related Tutorials