Cuts away everything after and including the first null in an array - CSharp System

CSharp examples for System:Array Null Element

Description

Cuts away everything after and including the first null in an array

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*  ww w .  j  a  v a2 s .co m*/

public class Main{
        /// <summary>
      /// Cuts away everything after and including the first null
      /// </summary>
      /// <typeparam name="T"></typeparam>
      /// <param name="arr"></param>
      public static void Trunc<T>(ref T[] arr)
         where T : class
      {
         var lastIndex = arr.Length - 1;
         for (var i = 0; i <= lastIndex; i++)
         {
            if (arr[i] != null && i != lastIndex)
            {
               Array.Resize(ref arr, i);
               break;
            }
         }
      }
}

Related Tutorials