Adds the given value to the first slot that is not occupied in the given array - CSharp System

CSharp examples for System:Array Element Add

Description

Adds the given value to the first slot that is not occupied in the given array

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/* ww w .java 2 s .c o  m*/

public class Main{
        /// <summary>
      /// Adds the given value to the first slot that is not occupied in the given array
      /// </summary>
      /// <returns>The index at which it was added</returns>
      public static uint Add<T>(ref T[] arr, T val)
      {
         var index = arr.GetFreeIndex();

         // no free space found: Make more space
         if (index >= arr.Length)
         {
            EnsureSize(ref arr, (int)(index * LoadConstant) + 1);
         }
         arr[index] = val;
         return index;
      }
        public static uint GetFreeIndex<T>(this T[] arr)
      {
         uint i = 0;
         for (; i < arr.Length; i++)
         {
            if (arr[i] == null || arr[i].Equals(default(T)))
            {
               return i;
            }
         }
         return i;
      }
}

Related Tutorials