Example #1
0
        static void Main(string[] args)
        {
            var storeWithDelegate = new DataStoreWithDelegate(data: new int[] { 1, 3, 5, 3, 4, 2, 7, 6 });

            var sum = storeWithDelegate.Process(SumOfOdd);

            Console.WriteLine($"Páratlan számok összege: {sum}");

            sum = storeWithDelegate.Process(ProductOfEven);
            Console.WriteLine($"Páros számok szorzata: {sum}");


            //func használata
            sum = storeWithDelegate.Process2(SumOfOdd);
            Console.WriteLine($"Páratlan számok összege: {sum}");

            sum = storeWithDelegate.Process3(data => {
                var s = 0;
                foreach (var d in data)
                {
                    if (d % 2 == 1)
                    {
                        s = +d;
                    }
                }

                return(s);
            });
            Console.WriteLine($"Páratlan számok összege: {sum}");

            Console.ReadLine();
        }
Example #2
0
        static void Main(string[] args)
        {
            var storeWithDelegate = new DataStoreWithDelegate(data: new int[] { 1, 3, 4, 5, 7, 8, 10, 15, 30 });

            #region 1. type of using delegate
            // have to do for using:
            // 1. You need to define a delegate type (ProcessDef) (called page)
            // 2. Define a function corresponding to the delegate (SumOfOdd) (call page)
            // 3. The function must be received with a variable at the point of use Process (ProcessDef strategy) (called page)
            // 4. The function must be sent for use (call page)
            var sum = storeWithDelegate.Process(SumOfOdd);

            Console.WriteLine($"1. type of using delegate");  // 31
            Console.WriteLine($"Sum odd numbers: {sum}\r\n"); // 31
            #endregion 1. type of using delegate



            #region 2. type of using delegate
            // On the called page instead of the delegate + parameter pickup using the Func <> definition in the Process2 function.
            // 1. A corresponding function must be converted: Func <int [], int> (called page)
            // 2. Define a function corresponding to the delegate (SumOfOdd) (call page)
            // 3. The function must be sent for use (call page)
            sum = storeWithDelegate.Process2(SumOfOdd);

            Console.WriteLine($"2. type of using delegate");  // 31
            Console.WriteLine($"Sum odd numbers: {sum}\r\n"); // 31
            #endregion 2. type of using delegate



            #region 3. type of using delegate
            // 1. A corresponding function must be converted: Func <int [], int> (called page)
            // 2. We will send the function LAMBDA defined within the line.
            sum = storeWithDelegate.Process3(data =>
            {
                var s = 0;
                foreach (var d in data)
                {
                    //We look for odd numbers.
                    // % is the sign of the integer dividing
                    // if divided by 2 we get 1 left, the number is odd
                    if (d % 2 == 1)
                    {
                        s += d;
                    }
                }
                return(s);
            });

            Console.WriteLine($"3. type of using delegate");  // 31
            Console.WriteLine($"Sum odd numbers: {sum}\r\n"); // 31
            #endregion 3. type of using delegate


            Console.ReadLine();
        }
Example #3
0
        static void Main(string[] args)
        {
            var storeWithDelegate = new DataStoreWithDelegate(data: new int[] { 1, 3, 4, 5, 7, 8, 10, 15, 30 });

            ///1. definiálni kell egy delegate típust (ProcessDef) (Hívott oldal)
            ///2. definiálni kell egy függvényt, ami megfelel a delegate-nek (SumOfOdd) (hívó oldal)
            ///3. fogadni kell a felhasználás helyén egy változóval a függvényt Process(ProcessDef strategy) (hívott oldal)
            ///4. be kell küldeni a függvényt felhasználásra (hívó oldal)
            var sum = storeWithDelegate.Process(SumOfOdd);

            Console.WriteLine($"Páratlanok összege: {sum}");

            //Első egyszerűsítés: hívott oldalon delegate + paraméter átvétel helyett Func<> definíció a Process2 függvényben
            ///1. át kell venni egy megfelelő függvényt: Func<int[], int> (Fogadó oldal)
            ///2. definiálni kell egy függvényt, ami megfelel a delegate-nek (SumOfOdd) (hívó oldal)
            ///3. be kell küldeni a függvényt felhasználásra (hívó oldal)
            sum = storeWithDelegate.Process2(SumOfOdd);
            Console.WriteLine($"Páratlanok összege: {sum}");

            //Második egyszerűsítés
            //1. át kell venni egy megfelelő függvényt: Func<int[], int> (Fogadó oldal)
            //2. beküldjük a LAMBDA kifejezéssel a soron belül definiált függvényünket
            sum = storeWithDelegate.Process3(data =>
            {
                var s = 0;
                foreach (var d in data)
                {
                    //% az egész osztás jele, ha 2-vel osztva maradékul 1-et ad, akkor páratlan a szám
                    if (d % 2 == 1)
                    {
                        s += d;
                    }
                }

                return(s);
            });

            Console.WriteLine($"Páratlanok összege: {sum}");


            Console.ReadLine();
        }