Cuts away all null values from array - CSharp System

CSharp examples for System:Array Null Element

Description

Cuts away all null values from array

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from  w  w  w  .  j a va  2s .co  m*/

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

Related Tutorials