Slice a subset of the provided array. - CSharp System

CSharp examples for System:Array Slice

Description

Slice a subset of the provided array.

Demo Code


using System.Linq;
using System;/*from   w w  w  .  j ava  2s . co m*/
using System.Collections.Generic;
using System.Collections;
using UnityEngine;

public class Main{
        /// <summary>
        /// Slice a subset of the provided array. 
        /// </summary>
        public static T[] Subset<T>(this T[] source, int length)
        {
            //Garbage in, garbage out. 
            if (source == null)
            {
                return null;
            }

            //Ensure bounds. 
            length = Mathf.Clamp(length, 0, source.Length);

            //Generate our new array. 
            T[] toReturn = new T[length];
            //Copy our source into our new array. 
            Array.Copy(source, 0, toReturn, 0, length);

            return toReturn;
        }
}

Related Tutorials