Create a new array by prepending the given elements. - CSharp System

CSharp examples for System:Array Create

Description

Create a new array by prepending the given elements.

Demo Code

// Permission is hereby granted, free of charge, to any person obtaining a copy
using System;/* w w  w  .  j a  v  a  2 s. c o  m*/

public class Main{
        /// <summary>
      ///   Create a new array by prepending the given elements.
      /// </summary>
      public static T[] Prepend<T> (T[] array, T element1, T element2)
      {
         T[] result = new T[array.Length + 2];
         Array.Copy (array, 0, result, 2, array.Length);
         result [0] = element1;
         result [1] = element2;
         return result;
      }
        /// <summary>
      ///   Create a new array by prepending the given element.
      /// </summary>
      public static T[] Prepend<T> (T[] array, T element)
      {
         T[] result = new T[array.Length + 1];
         Array.Copy (array, 0, result, 1, array.Length);
         result [0] = element;
         return result;
      }
}

Related Tutorials