C#委託 delegate

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test1
{
    class Program
    {
        public  delegate double MProcess(double a, double b);
        public static double Add(double a, double b)
        {
            return a + b;
        }
        public static double Multity(double a, double b)
        {
            return a * b;
        }
        static void Main(string[] args)
        {
            while (true)
            {
                int choose = Convert.ToInt32(Console.ReadLine());
                MProcess process;
                if (choose == 1)//賦值方式 
                {
                    process = Add;
                }
                else if(choose == 2)
                {
                    process = Multity;
                }
                else if (choose == 11)//new 方式
                {
                    process = new MProcess(Add);
                }
                else
                {
                    process = new MProcess(Multity);
                }
                string r = Console.ReadLine();
                string[] pars = r.Split(new char[1] { ' ' });
                int a = Convert.ToInt32(pars[0]);
                int b = Convert.ToInt32(pars[1]);
                Console.WriteLine("Result : {0}", process(a, b));

            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章