Remove First from IList by condition - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Remove First from IList by condition

Demo Code


using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;/*from  w w w  . j  ava2  s .c o m*/

public class Main{
        public static T RemoveFirst<T>(this IList<T> items, Func<T, bool> filter)
      {
         for (var i = 0; i < items.Count; i++)
         {
            var item = items[i];
            if (filter(item))
            {
               items.RemoveAt(i);
               return item;
            }
         }
         return default(T);
      }
}

Related Tutorials