Пример #1
0
        private void BtnDelegate_Click(object sender, EventArgs e)
        {
            rtbOutput.Text = string.Empty;

            //Parameters to be used within the delegate  instantiations.
            int firstNumber, secondnumber;

            //Here we instantiate doCalc and point to the methods AddNums, and subtract respectively with delegate b and c

            //Recal that doCalc requires parameters int, int. ( delegate doCalc(int number1, int number2) )
            doCalc b = addNums;
            doCalc c = Subtract;

            if (int.TryParse(txtNumOneInput.Text, out firstNumber) == true && int.TryParse(txtNum2Input.Text, out secondnumber) == true)
            {
                //Here when we need to call the delegates and run them for a result its essentially this
                //you call your instantiaton of your delegate in this case b, and then insert parameters that would've been
                //inserted into the provided methods otherwise.

                //delegateVariable[method parameters of method provided]
                //equivalant to addNums(firstNumber, secondNumber)
                rtbOutput.Text = $"Adding Numbers: {b(firstNumber, secondnumber)}\r\n";

                //Same thing here for delegate b, but doing this for c with the subtract method rather than the addNums
                //Method
                rtbOutput.Text = $"Subtracting Numbers: {c(firstNumber, secondnumber)}";
            }
            else
            {
                MessageBox.Show("Invalid input", "Number inputs");
                rtbOutput.Text = "Error";
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("임의의 두 수를 입력하십시오. ex.(1,2)");
            int[] result = Array.ConvertAll((Console.ReadLine()).Split(','), i => int.Parse(i));


            doCalc Calc = (doCalc)Delegate.Combine(
                new doCalc(Add),
                new doCalc(Substract),
                new doCalc(Multiple),
                new doCalc(Divide)
                );

            Calc(result[0], result[1]);
        }