Insert element to an array - CSharp System

CSharp examples for System:Array Element Add

Description

Insert element to an array

Demo Code


using System.Collections.Generic;
using System.Collections;
using System;/*from  w w w.  j  a va2 s  . c om*/

public class Main{
    public static void Insert<T>(ref T[] array, int index, T item)
      {
         ArrayList arrayList = new ArrayList();
         arrayList.AddRange(array);
         arrayList.Insert(index, item);
         array = (arrayList.ToArray(typeof(T)) as T[]);
      }
    public static void AddRange<T>(ref T[] array, T[] items)
      {
         int num = array.Length;
         Array.Resize<T>(ref array, array.Length + items.Length);
         for (int i = 0; i < items.Length; i++)
         {
            array[num + i] = items[i];
         }
      }
}

Related Tutorials