Custom Where Generic Extension Method – why yield ??

Key points

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

namespace MyownExtensionMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            var employees = new List<Employee>
            {
                new Employee {Name="Ashok",Salary=1000},
                new Employee {Name="Abhishek",Salary=20000},
                new Employee {Name="Abhay",Salary=15000},
                new Employee {Name="Sushant",Salary=8000},
            };


            var filterEmployees = employees.Where(emp => emp.Salary > 5000);
            //foreach (var emp in filterEmployees)
            //{
            //    Console.WriteLine(emp.Name);
            //}

            //Console.WriteLine("==============================");
            ///* Below is my extension of where method  */

            //var filterExtensionWhereEmp = employees.MyWhereInitial(emp => emp.Salary > 5000);
            //foreach (var emp in filterExtensionWhereEmp)
            //{
            //    Console.WriteLine(emp.Name);
            //}

            Console.ReadLine();
        }
    }

    static class ListEmployeeExtension
    {

        public static IEnumerable<T> MyWhereInitial<T>(this List<T> employees,
              Func<T, bool> predicate
              )
        {
            var finallist = new List<T>();
            foreach (var item in employees)
            {
                if (predicate(item))
                    finallist.Add(item);
            }
            return finallist;
        }

        public static IEnumerable<T> MyWhereFinal<T>(this List<T> employees,
            Func<T, bool> predicate
            )
        {
            foreach (var item in employees)
            {
                if (predicate(item))
                    yield return item;
            }
        }
    }

    class Employee
    {
        int _salary;
        public int Salary
        {
            get
            {
                Console.WriteLine($"My name is {Name} & my salary is {_salary}");
                return _salary;
            }
            set
            {
                _salary = value;
            }
        }
        public string Name { get; set; }
    }
}

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

namespace MyownExtensionMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            var employees = new List<Employee>
            {
                new Employee {Name="Ashok",Salary=1000},
                new Employee {Name="Abhishek",Salary=20000},
                new Employee {Name="Abhay",Salary=15000},
                new Employee {Name="Sushant",Salary=8000},
            };


            //var filterEmployees = employees.Where(emp => emp.Salary > 5000);
            //foreach (var emp in filterEmployees)
            //{
            //    Console.WriteLine(emp.Name);
            //}

            //Console.WriteLine("==============================");
            ///* Below is my extension of where method  */

            var filterExtensionWhereEmp = employees.MyWhereInitial(emp => emp.Salary > 5000);
            //foreach (var emp in filterExtensionWhereEmp)
            //{
            //    Console.WriteLine(emp.Name);
            //}

            Console.ReadLine();
        }
    }

    static class ListEmployeeExtension
    {

        public static IEnumerable<T> MyWhereInitial<T>(this List<T> employees,
              Func<T, bool> predicate
              )
        {
            var finallist = new List<T>();
            foreach (var item in employees)
            {
                if (predicate(item))
                    finallist.Add(item);
            }
            return finallist;
        }

        public static IEnumerable<T> MyWhereFinal<T>(this List<T> employees,
            Func<T, bool> predicate
            )
        {
            foreach (var item in employees)
            {
                if (predicate(item))
                    yield return item;
            }
        }
    }

    class Employee
    {
        int _salary;
        public int Salary
        {
            get
            {
                Console.WriteLine($"My name is {Name} & my salary is {_salary}");
                return _salary;
            }
            set
            {
                _salary = value;
            }
        }
        public string Name { get; set; }
    }
}

				
			

My name is Ashok & my salary is 1000
My name is Abhishek & my salary is 20000
My name is Abhay & my salary is 15000
My name is Sushant & my salary is 8000

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

namespace MyownExtensionMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            var employees = new List<Employee>
            {
                new Employee {Name="Ashok",Salary=1000},
                new Employee {Name="Abhishek",Salary=20000},
                new Employee {Name="Abhay",Salary=15000},
                new Employee {Name="Sushant",Salary=8000},
            };


            //var filterEmployees = employees.Where(emp => emp.Salary > 5000);
            //foreach (var emp in filterEmployees)
            //{
            //    Console.WriteLine(emp.Name);
            //}

            //Console.WriteLine("==============================");
            ///* Below is my extension of where method  */

            var filterExtensionWhereEmp = employees.MyWhereInitial(emp => emp.Salary > 5000);
            //foreach (var emp in filterExtensionWhereEmp)
            //{
            //    Console.WriteLine(emp.Name);
            //}

            Console.ReadLine();
        }
    }

    static class ListEmployeeExtension
    {

        public static IEnumerable<T> MyWhereInitial<T>(this List<T> employees,
              Func<T, bool> predicate
              )
        {
            var finallist = new List<T>();
            foreach (var item in employees)
            {
                if (predicate(item))
                    finallist.Add(item);
            }
            return finallist;
        }

        public static IEnumerable<T> MyWhereFinal<T>(this List<T> employees,
            Func<T, bool> predicate
            )
        {
            foreach (var item in employees)
            {
                if (predicate(item))
                    yield return item;
            }
        }
    }

    class Employee
    {
        int _salary;
        public int Salary
        {
            get
            {
                Console.WriteLine($"My name is {Name} & my salary is {_salary}");
                return _salary;
            }
            set
            {
                _salary = value;
            }
        }
        public string Name { get; set; }
    }
}

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

namespace MyownExtensionMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            var employees = new List<Employee>
            {
                new Employee {Name="Ashok",Salary=1000},
                new Employee {Name="Abhishek",Salary=20000},
                new Employee {Name="Abhay",Salary=15000},
                new Employee {Name="Sushant",Salary=8000},
            };


            //var filterEmployees = employees.Where(emp => emp.Salary > 5000);
            //foreach (var emp in filterEmployees)
            //{
            //    Console.WriteLine(emp.Name);
            //}

            //Console.WriteLine("==============================");
            ///* Below is my extension of where method  */

            var filterExtensionWhereEmp = employees.MyWhereFinal(emp => emp.Salary > 5000);
            foreach (var emp in filterExtensionWhereEmp)
            {
                Console.WriteLine(emp.Name);
            }

            Console.ReadLine();
        }
    }

    static class ListEmployeeExtension
    {

        public static IEnumerable<T> MyWhereInitial<T>(this List<T> employees,
              Func<T, bool> predicate
              )
        {
            var finallist = new List<T>();
            foreach (var item in employees)
            {
                if (predicate(item))
                    finallist.Add(item);
            }
            return finallist;
        }

        public static IEnumerable<T> MyWhereFinal<T>(this List<T> employees,
            Func<T, bool> predicate
            )
        {
            foreach (var item in employees)
            {
                if (predicate(item))
                    yield return item;
            }
        }
    }

    class Employee
    {
        int _salary;
        public int Salary
        {
            get
            {
                Console.WriteLine($"My name is {Name} & my salary is {_salary}");
                return _salary;
            }
            set
            {
                _salary = value;
            }
        }
        public string Name { get; set; }
    }
}

				
			

My name is Ashok & my salary is 1000
My name is Abhishek & my salary is 20000
Abhishek
My name is Abhay & my salary is 15000
Abhay
My name is Sushant & my salary is 8000
Sushant

Leave a Comment