LinQ methotlarını uzatılmış ve generic metodlarla yazma

Merhaba arkadalar be bu makalede bazı linq deyimlerinin yaptığı işlemleri yapan yöntemler yazacağım. Burada amacım generic metod ve extention methodları daha iyi kavramak. Linq sorgusunda ben bir anlamda where karşılık gelen bir yöntem yazıyorum.

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);

Normalde metadatasına baktığımızda görüldüğü gibi where deyimi aşırı yüklenmiştir. Parametresi Func<> tipinden bir delegedir. Ben burada bir değişiklik yapıp Predicate<T> tipinde bir parametre geçiyorum. Bu delege Func<T,Bool> delegesinin nerdeyse aynısıdır.

 

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] dizim = { 4, 6, 7, 8, 90, 6, 10 };
            foreach (var item in dizim.Bul(x => x % 2 == 0))
                Console.WriteLine(item);
        }
    }

    public static class ee
    {
        public static IEnumerable<T> Bul<T>(this IEnumerable<T> deger, Predicate<T> sunum) //uzatılmış bir bul yöntemi
        {
            foreach (var item in deger)
            {
                if (sunum(item))
                    yield return item;
            }
        }
    }
}

z

z

z

 

Add comment

The file '/Custom/Widgets/Calendar/widget.cshtml' does not exist.The file '/Custom/Widgets/Category list/widget.cshtml' does not exist.The file '/Custom/Widgets/Tag cloud/widget.cshtml' does not exist.The file '/Custom/Widgets/Page List/widget.cshtml' does not exist.The file '/Custom/Widgets/Month List/widget.cshtml' does not exist.