DEV Community

Nosirbek
Nosirbek

Posted on • Updated on

C# Predicate

Program.cs

using Predicate;
public class Program
{
    public static void Main(string[] args)
    {
        var yoshlar = new int[] {16,21,13,41,15,27,23};

        var yosh = new List<int>();

        yoshlar.KottaBollar(KattaBola)
            .ToList()
            .ForEach(System.Console.WriteLine);
    }

    public static bool KattaBola(int age)
    {
        return age > 18;
    }

    public static bool ToqYokiJuft(int num)
    {
        return num % 2 != 0;
    }
}
Enter fullscreen mode Exit fullscreen mode

LinqExtensions.cs

using System.Collections;
using System.Collections.Generic;

namespace Predicate;

public static class LinqExtansions
{
    public delegate bool Predicate<T1>(T1 arg1);

    public static IEnumerable<T1> KottaBollar<T1>(this IEnumerable<T1> bollar, Predicate<T1> callback)
    {
        // var result = new List<T1>();

        foreach(var item in bollar)
        {
            if(callback.Invoke(item))
            {
                // result.Add(item);

                yield return item;
            }

        }
        // return result;
    }
}
Enter fullscreen mode Exit fullscreen mode

Result

18 - yoshdan kattalarni qaytaradi .

21
41
27
23
Enter fullscreen mode Exit fullscreen mode

Top comments (0)