Example #1
0
        public static bool Find <T>(this SCG.IEnumerable <T> enumerable, Func <T, bool> predicate, out T item)
        {
            #region Code Contracts

            // Argument must be non-null
            Requires(enumerable != null, ArgumentMustBeNonNull);

            // Argument must be non-null
            Requires(predicate != null, ArgumentMustBeNonNull);


            // Returns true if p(x) returned true for some item x; otherwise false
            Ensures(Result <bool>() == enumerable.Any(predicate));

            // Result item equals the first (or default) item satisfying the predicate
            Ensures(ValueAtReturn(out item).IsSameAs(enumerable.FirstOrDefault(predicate)));

            #endregion

            bool result;

            // Use Enumerable.Where() to be able to retrieve value for result
            using (var enumerator = enumerable.Where(predicate).GetEnumerator()) {
                item = (result = enumerator.MoveNext()) ? enumerator.Current : default(T);
            }

            return(result);
        }