DEV Community

Nosirbek
Nosirbek

Posted on • Edited on

4 3

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay