Try Get from IEnumerable by Predicate - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Try Get from IEnumerable by Predicate

Demo Code


using System.Collections.Generic;
using System.Collections;
using System;//  w ww  .j  a  va 2s.c  om

public class Main{
        public static bool TryGet<T>(IEnumerable list, Predicate<T> match, out T result) where T : class
    {
      foreach (T item in list)
      {
        if (match(item))
        {
          result = item;
          return true;
        }
      }
      result = null;
      return false;
    }
}

Related Tutorials