Answer to Question #180521 in C# for Miley

Question #180521

Extension methods on IEnumerable<T>. All of them taking a delegate

1.       CustomAll - Should work as All operation of Linq, with custom logic passed as delegate

2.       CustomAny - Should work as Any operation of Linq, with custom logic passed as delegate

3.       CustomMax - Should work as Max operation of Linq, with custom logic passed as delegate

4.       CustomMin - Should work as Min operation of Linq, with custom logic passed as delegate

5.       CustomWhere - Should work as Where operation of Linq, with custom logic passed as delegate

6.       CustomSelect - Should work as Select operation of Linq, with custom logic passed as delegate

 


1
Expert's answer
2021-04-17T01:29:44-0400

Solution is:

using System;
using System.Collections.Generic;


namespace IEnum
{
    class Program
    {
        static void Main(string[] args)
        {
            var array = new int[5] {2, 3, 4, 5, 6};
            Console.WriteLine(array.CustomAll(x => x > 1));
            Console.WriteLine(array.CustomAll(x => x > 1));
            Console.WriteLine(array.CustomAny(x => x < 0));
            Console.WriteLine(array.CustomMax(x => x * 2));
            Console.WriteLine(array.CustomMin(x => x - 1));
            Console.WriteLine(string.Join(", ", array.CustomWhere(x => x > 3)));
            Console.WriteLine(string.Join(", ", array.CustomSelect(x => x + 1)));
        }
    }


    public static class EnumerableExtensions
    {
        public static bool CustomAll<T>(this IEnumerable<T> enumerable, Func<T, bool> func)
        {
            if (enumerable is null)
                throw new ArgumentNullException(nameof(enumerable));


            foreach (var elem in enumerable)
            {
                if (!func(elem))
                    return false;
            }


            return true;
        }


        public static bool CustomAny<T>(this IEnumerable<T> enumerable, Func<T, bool> func)
        {
            if (enumerable is null)
                throw new ArgumentNullException(nameof(enumerable));


            foreach (var elem in enumerable)
            {
                if (func(elem))
                    return true;
            }


            return false;
        }


        public static TResult CustomMax<T, TResult>(this IEnumerable<T> enumerable, Func<T, TResult> func) where TResult : IComparable<TResult>
        {
            if (enumerable is null)
                throw new ArgumentNullException(nameof(enumerable));


            TResult max;


            using var enumerator = enumerable.GetEnumerator();
            if (enumerator.MoveNext())
            {
                max = func(enumerator.Current);
            }
            else throw new InvalidOperationException("Sequence contains no elements");


            while (enumerator.MoveNext())
            {
                var result = func(enumerator.Current);
                if (max is null || max.CompareTo(result) < 0)
                    max = result;
            }


            return max;
        }


        public static TResult CustomMin<T, TResult>(this IEnumerable<T> enumerable, Func<T, TResult> func) where TResult : IComparable<TResult>
        {
            if (enumerable is null)
                throw new ArgumentNullException(nameof(enumerable));


            TResult min;


            using var enumerator = enumerable.GetEnumerator();
            if (enumerator.MoveNext())
            {
                min = func(enumerator.Current);
            }
            else throw new InvalidOperationException("Sequence contains no elements");


            while (enumerator.MoveNext())
            {
                var result = func(enumerator.Current);
                if (min is null || min.CompareTo(result) > 0)
                    min = result;
            }


            return min;
        }


        public static IEnumerable<T> CustomWhere<T>(this IEnumerable<T> enumerable, Func<T, bool> func)
        {
            if (enumerable is null)
                throw new ArgumentNullException(nameof(enumerable));


            foreach (var elem in enumerable)
            {
                if (func(elem))
                    yield return elem;
            }
        }


        public static IEnumerable<TResult> CustomSelect<T, TResult>(this IEnumerable<T> enumerable, Func<T, TResult> func)
        {
            if (enumerable is null)
                throw new ArgumentNullException(nameof(enumerable));


            foreach (var elem in enumerable)
            {
                yield return func(elem);
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS