Cuts away all null values from generic array - CSharp System

CSharp examples for System:Array Null Element

Description

Cuts away all null values from generic array

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from www  .  j  a  v a  2s.  com

public class Main{
        /// <summary>
      /// Cuts away all null values
      /// </summary>
      public static void PruneVals<T>(ref T[] arr) where T : struct
      {
         var list = new List<T>(arr.Length);
         foreach (var obj in arr)
         {
            if (!obj.Equals(default(T)))
            {
               list.Add(obj);
            }
         }
         arr = list.ToArray();
      }
}

Related Tutorials