Esempio n. 1
0
        static void Main(string[] args)
        {
            SomeOperation add    = Sum;
            int           result = add(10, 10);

            Console.WriteLine(result);

            Func <int, int, int> addFunc = Sum;

            result = addFunc(10, 10);
            Console.WriteLine(result);

            Func <int> getRandomNumber = delegate()
            {
                Random rnd = new Random();
                return(rnd.Next(1, 100));
            };

            Console.WriteLine("Random number: " + getRandomNumber());

            Func <int> getRndNumber = () => new Random().Next(1, 100);

            Console.WriteLine("Random number: " + getRndNumber());

            Func <int, int, int> Suma = (x, y) => x + y;

            Console.WriteLine("Sum = " + Suma(12, 23));
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            SomeOperation add = Sum;

            int result = add(10, 10);

            Console.WriteLine(result);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            SomeOperation add    = new SomeOperation(Sum); // sau mai puteam SomeOperation add=Sum;
            int           result = add(10, 20);

            Console.WriteLine(result);

            Func <int, int, int> adauga = Sum;
            int rezultat = adauga(32, 21);

            Console.WriteLine(rezultat);

            //Func with Anonymous Method
            Func <int> RandomNumber = delegate()
            {
                Random random = new Random();
                return(random.Next(1, 100));
            };
            var r  = RandomNumber;
            int an = r();

            Console.WriteLine("Random Number " + r); //System.Func`1[System.Int32]
            Console.WriteLine("Random Number1 " + an);
            //Func with lambda expression
            Func <int> getRandomNumber = () => new Random().Next(1, 100);

            var a = getRandomNumber;

            Console.WriteLine("Random Number2: " + a());

            //Or Func with lambda expression 2

            Func <int, int, int> Suma = (x, y) => x + y;
            int b = Suma(12, 14);

            Func <string, int, string[]> extractMeth = delegate(string s, int i)
            {
                char[] delimiters = new char[] { ' ' };
                return(i > 0 ? s.Split(delimiters, i) : s.Split(delimiters));
            };

            string title = "The Scarlet Letter";

            // Use Func instance to call ExtractWords method and display result
            foreach (string word in extractMeth(title, 2))
            {
                Console.WriteLine(word);
            }
            foreach (string word in extractMeth(title, 4))
            {
                Console.WriteLine(word);
            }
        }
Esempio n. 4
0
    public static void Calculate(int choice, double numOne, double numTwo)
    {
        IDictionary <int, string> allActionsDictionary = new Dictionary <int, string>()
        {
            { 1, "adding" },
            { 2, "subtracting" },
            { 3, "multiplying" },
            { 4, "dividing" }
        };

        switch (choice)
        {
        case 1:
        {
            SomeOperation addNumbers = Delegate.Add;
            var           result     = addNumbers(numOne, numTwo);
            ShowTheResults.Show(allActionsDictionary[1], numOne, numTwo, result);
            break;
        }

        case 2:
        {
            SomeOperation subtractNumbers = Delegate.Subtract;
            var           result          = subtractNumbers(numOne, numTwo);
            ShowTheResults.Show(allActionsDictionary[2], numOne, numTwo, result);
            break;
        }

        case 3:
        {
            SomeOperation multiplyNumbers = Delegate.Multiply;
            var           result          = multiplyNumbers(numOne, numTwo);
            ShowTheResults.Show(allActionsDictionary[3], numOne, numTwo, result);
            break;
        }

        case 4:
        {
            SomeOperation divideNumbers = Delegate.Divide;
            var           result        = divideNumbers(numOne, numTwo);
            ShowTheResults.Show(allActionsDictionary[4], numOne, numTwo, result);
            break;
        }

        default:
        {
            Console.WriteLine("There is no such a method!");
            break;
        }
        }
    }
Esempio n. 5
0
    public static void Calculate(int choice, double numOne, double numTwo)
    {
        switch (choice)
        {
        case 1:
        {
            SomeOperation addNumbers = Delegate.Add;
            var           result     = addNumbers(numOne, numTwo);
            ShowTheResults.Show("adding", numOne, numTwo, result);
            break;
        }

        case 2:
        {
            SomeOperation subtractNumbers = Delegate.Subtract;
            var           result          = subtractNumbers(numOne, numTwo);
            ShowTheResults.Show("subtracting", numOne, numTwo, result);
            break;
        }

        case 3:
        {
            SomeOperation multiplyNumbers = Delegate.Multiply;
            var           result          = multiplyNumbers(numOne, numTwo);
            ShowTheResults.Show("multiplying", numOne, numTwo, result);
            break;
        }

        case 4:
        {
            SomeOperation divideNumbers = Delegate.Divide;
            var           result        = divideNumbers(numOne, numTwo);
            ShowTheResults.Show("dividing", numOne, numTwo, result);
            break;
        }

        default:
        {
            Console.WriteLine("There is no such a method!");
            break;
        }
        }
    }
Esempio n. 6
0
        private static void Functions()
        {
            SomeOperation add = Sum;

            int result = add(10, 10);

            Console.WriteLine(result);

            Func <int, int, int> add2 = Sum;

            var result2 = add2(1, 2);

            Func <int, int, int> add3 = delegate(int a, int b) { return(a + b); };

            Func <int, int, int> add4 = (int a, int b) => { return(a + b); };

            Func <int, int, int> add5 = (a, b) => { return(a + b); };

            Func <int, int, double> division = (a, b) => { return((double)a / b); };
        }
        static void Main(string[] args)
        {
            #region Info

            /*
             * - C# includes built-in generic delegate types Func, Action and Predicate, so that you don't need to define custom delegates
             * - Predicate is built-in delegate type defined in the System namespace
             * - Predicate delegate has one input parameters and one out parameter
             * - Predicate delegate must return a bool value
             * - Predicate delegate can be used with an anonymous method or lambda expression
             */
            #endregion

            SomeOperation del1 = IsUpperCase;
            Console.WriteLine($"del1 - IsUpperCase(UP) = {del1("UP")}");

            Predicate <string> predDel1 = IsUpperCase;
            Console.WriteLine($"predDel1 - IsUpperCase(Up) = {predDel1("Up")}");

            SomeOperation del2 = IsPalindrom;

            Console.WriteLine(del2("Madam"));
            Console.ReadLine();
        }
Esempio n. 8
0
        //Func is built-in delegate type.
        //Func delegate type must return a value.
        //Func delegate type can have zero to 16 input parameters.
        //Func delegate does not allow ref and out parameters.
        //Func delegate type can be used with an anonymous method or lambda expression.

        static void Main(string[] args)
        {
            SomeOperation add = Sum;

            int result = add(2, 3);

            Console.WriteLine(result);

            Func <int, int, int> add_v2 = Sum;
            int result_2 = add_v2(10, 20);

            Console.WriteLine(result_2);

            Func <int> getRandomNumber = delegate()
            {
                Random rnd = new Random();
                return(rnd.Next(1, 100));
            };

            Console.WriteLine(getRandomNumber.Invoke());



            Func <int> getRandomNumber_2 = () => new Random().Next(1, 100);

            Console.WriteLine(getRandomNumber_2.Invoke());

            Func <int, int, int> sum = (x, y) => x + y;

            Console.WriteLine(sum.Invoke(200, 300));


            Func <int, int, int, DateTime> createDateTime = (day, month, year) => new DateTime(year, month, day);

            Console.WriteLine(createDateTime(1, 9, 2019));
        }