예제 #1
0
        static void Main()
        {
            //source of data
            List <int> numbers = new List <int> {
                1, 4, 2, 5, 6, 3, 0, 7, 9, 8
            };

            //create delegate to refer a method
            //LogicInvoker logicDel = new LogicInvoker(new Logic().IsOdd);

            //LogicInvoker logicDel = delegate (int num)
            //  {
            //      return num > 3 ? true : false;
            //  };
            //Lambda expression: representing anonymous method
            LogicInvoker logicDel = num => num > 3;

            //pass source of data to be filtered and the logic through a delegate object to filter
            List <int> output = Filter(numbers, logicDel);

            foreach (var item in output)
            {
                Console.WriteLine(item);
            }

            SomeDel sdAdd = delegate(int x, int y)
            {
                return(x + y);
            };
            SomeDel sdSubtract = (a, b) => (a - b);
        }
        static void Main()
        {
            List <int> dataSource = new List <int> {
                1, 3, 2, 4, 6, 7, 5, 9, 0, 8
            };
            //LogicInvoker<int> logic = new LogicInvoker<int>(new Logic().IsGreater);

            //anonymous method
            //LogicInvoker<int> logic = delegate (int num)
            //{
            //    return num > 2 && num < 8 ? true : false;
            //};
            //LogicInvoker<int> logic = new LogicHolder().Test;

            //Lambda expression: new syntax to wtite anonymous method
            LogicInvoker <int> logicEven    = (num) => num % 2 == 0;
            LogicInvoker <int> logicOdd     = (num) => num % 2 != 0;
            LogicInvoker <int> logicGreater = (num) => num > 5;

            List <int> result = Filter <int>(dataSource, logicGreater);

            foreach (int item in result)
            {
                Console.WriteLine(item);
            }

            List <Product> products = new List <Product>
            {
                new Product {
                    Name = "dell xps", Id = 2, Price = 67000, Description = "new laptop from dell"
                },
                new Product {
                    Name = "hp probook", Id = 1, Price = 43000, Description = "new laptop from hp"
                },
                new Product {
                    Name = "lenovo thinkpad", Id = 3, Price = 54000, Description = "new laptop from lenovo"
                }
            };
            //products.Sort(new ProductComparison());
            //Comparison<Product> comparisonLogic = delegate (Product x, Product y)
            //{
            //    return x.Name.CompareTo(y.Name);
            //};
            Comparison <Product> comparisonLogic = (x, y) =>
                                                   x.Name.CompareTo(y.Name);

            products.Sort(comparisonLogic);
            //replace null by providing logic through anonymous method written using lambda expression
            //LogicInvoker<Product> logic = (p) => p.Price > 50000;
            LogicInvoker <Product> logic            = p => p.Name.Contains('h');
            List <Product>         filteredProducts =
                Filter <Product>(products, logic);

            foreach (Product item in filteredProducts)
            {
                Console.WriteLine(item);
            }
        }
예제 #3
0
        static void Main()
        {
            Func <int, bool>     isEven = (n) => n % 2 == 0;
            Func <int, int, int> addFn  = (a, b) => a + b;
            Action print = () => Console.WriteLine("hello");
            //Predicate<int>


            List <int> source = new List <int> {
                1, 4, 2, 5, 3, 7, 8, 6, 0, 9
            };

            Logic logicHolder = new Logic();
            //LogicInvoker<int, bool> evenDelegate = new LogicInvoker<int, bool>(logicHolder.IsOdd);
            LogicInvoker <int, bool> evenDelegate = logicHolder.IsOdd;

            //bool res = evenDelegate.Invoke(12);
            //or
            //bool res = evenDelegate(12);
            //Console.WriteLine(res);

            //C# (2.0)
            //LogicInvoker greaterThanDel = delegate (int num)
            //{
            //    return num > 3 ? true : false;
            //};

            //Lambda Expression (C# 3.0 - 2007)
            //LogicInvoker greaterThanDel = (num) =>
            //{
            //    return num > 3 ? true : false;
            //};
            //LogicInvoker greaterThanDel = (num) => num % 2 != 0;

            //Console.WriteLine(greaterThanDel(4));

            var resultList = Filter(source, evenDelegate);

            //var resultList = Filter(source, greaterThanDel);
            //var resultList = Filter(source, num => num % 2 == 0);
            foreach (int item in resultList)
            {
                Console.WriteLine(item);
            }



            //evenDelegate += logicHolder.IsOdd;
            //bool res = evenDelegate.Invoke(12);
            //Console.WriteLine(res);

            LogicInvoker <int, int>      l1  = (a) => a * a;
            LogicInvoker <int, int, int> add = (a, b) => a + b;
        }
        //public class LogicHolder
        //{
        //    public bool Test(int num)
        //    {
        //        return num > 2 && num < 8 ? true : false;
        //    }
        //}

        static List <T> Filter <T>(List <T> input, LogicInvoker <T> invoker)
        {
            List <T> output = new List <T>();

            foreach (T item in input)
            {
                if (invoker(item))
                {
                    output.Add(item);
                }
            }
            return(output);
        }
예제 #5
0
        //public class Inner
        //{
        //    public static bool Ano(int num)
        //    {
        //        return num > 3 ? true : false;
        //    }
        //}
        static List <int> Filter(List <int> input, LogicInvoker <int, bool> del)
        {
            List <int> result = new List <int>();

            foreach (int item in input)
            {
                bool value = del(item);
                if (value)
                {
                    result.Add(item);
                }
            }
            return(result);
        }
예제 #6
0
        static List <int> Filter(List <int> inputList, LogicInvoker invoker)
        {
            List <int> filtered = new List <int>();

            foreach (var number in inputList)
            {
                bool success = invoker(number);
                if (success)
                {
                    filtered.Add(number);
                }
            }
            return(filtered);
        }