簡單使用委託的過程[一]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace 委託練習1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            cal obj = new cal(add);//
            int res = obj(10, 20);//
            Console.WriteLine("10+20={0}", res);//
            obj -= add;//與之前指定的方法解綁
            obj += sub;//重新指向一個新的方法
            Console.WriteLine("10+20={0}", res);//
            Console.ReadLine();
        }


        static int add(int a, int b)//根據委託定義一個具體的方法(加法功能)
        {
            return   a + b ;
        }
        static int sub(int a, int b)//根據委託定義一個具體的方法(減法功能)
        {
            return a - b;
        }


        public delegate int cal(int a, int b);//


    }

}




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