.Net基礎加強11

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   使用   for   sp   

複習:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net;using System.Text.RegularExpressions;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            WebClient wc = new WebClient();            string html=wc.DownloadString("http://search.51job.com/list/020000%252C00,%2B,%2B,%2B,%2B,%2B,asp.net,2,%2B.html?lang=c&stype=1&specialarea=00");            //<a href="http://search.51job.com/list/co,c,2356581,000000,10,1.html" class="coname" target="_blank" >上海寶尊電子商務有限公司</a>            MatchCollection matches = Regex.Matches(html, @"<a\s+href=""http://search.51job.com/list/co,c,\d{7},000000,10,1.html"".+>(.+)</a>",RegexOptions.ECMAScript);            foreach (Match item in matches)            {                Console.WriteLine(item.Groups[1].Value);            }            Console.WriteLine(matches.Count);            Console.ReadKey();        }    }}

委託

委託是一種自訂的資料類型(類)

可以通過委託來儲存方法,其實就是new一個委派物件。

不同類型的方法(方法的參數與傳回值不同),要建立對應的委託類型來儲存。

委託一般定義在namespace中。

因為委託也是對象,所以可以賦值為null,所以建議使用委託之前要進行非空校正。

多播委託

Action

Action<T>

Action<T1,T2>

Fun

Public delegate T M1Delegate<T>(T msg);

M1Delegate<string> md=;

委託的意義:

增減程式的擴充性,靈活性,代碼注入。自己寫控制項的時候大量使用到了委託,定義事件的時候會也會用到委託。

Lambda運算式:就是方法的定義。(就是一種簡化的方法定義,用運算式來表示)

事件:

三連擊按鈕

using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 三連擊{    public partial class ucTripleClick : UserControl    {        public ucTripleClick()        {            InitializeComponent();        }        public TripleClickDelegate TripleClickHandler;        int count = 0;        //這裡是按鈕的單擊事件        private void button1_Click(object sender, EventArgs e)        {            count++;            if (count >= 3)            {                count = 0;                //這裡表示三連擊事件被觸發後要執行的代碼                //這裡的代碼不能寫死,要由將來使用該控制項的使用者來決定                //這裡需要執行一段代碼(一個方法),而這個方法的具體內容現在不確定                //MessageBox.Show("三連擊");                //使用委託                if(TripleClickHandler!=null)                {                TripleClickHandler();                }            }        }    }}
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 三連擊{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            MessageBox.Show("OK");        }        private void ucTripleClick1_Load(object sender, EventArgs e)        {        }        private void Form1_Load(object sender, EventArgs e)        {            ucTripleClick1.TripleClickHandler = Do;        }        void Do()        {            MessageBox.Show("三連擊");        }    }}
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 三連擊{    public partial class Form2 : Form    {        public Form2()        {            InitializeComponent();        }        private void Form2_Load(object sender, EventArgs e)        {            ucTripleClick1.TripleClickHandler = Do1;        }        void Do1()        {            MessageBox.Show("how r u doing?");        }    }}

通過事件來實現三連擊

using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 通過事件來實現三連擊{    public partial class UCTripleClick : UserControl    {        public UCTripleClick()        {            InitializeComponent();        }        //聲明一個事件對象        public event TripleClickDelegate TripleClickHandler;        int count = 0;        private void button1_Click(object sender, EventArgs e)        {            count++;            if (count >= 3)            {                count=0;                //當三連擊後這裡要執行一段未知代碼                //使用事件步驟:                //1.定義一個委託                //2.通過該委託定義一個事件(加個event而已)                //3.呼叫事件的方式與調用委託的方式一樣                if (TripleClickHandler!=null)                {                TripleClickHandler();                }            }        }    }    public delegate  void TripleClickDelegate();}
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 通過事件來實現三連擊{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            //事件只能通過+=或者-=來賦值,這樣避免了“覆蓋”的問題            //ucTripleClick1.TripleClickHandler = Do1;            ucTripleClick1.TripleClickHandler += ucTripleClick1_TripleClickHandler;             ucTripleClick1.TripleClickHandler+=ucTripleClick1_TripleClickHandler1;        }        void ucTripleClick1_TripleClickHandler()        {            MessageBox.Show("事件處理常式中!");        }        void ucTripleClick1_TripleClickHandler1()        {            MessageBox.Show("事件處理常式中2!");        }        void Do1()        {            MessageBox.Show("Do111111111111111111111");        }        private void button1_Click(object sender, EventArgs e)        {        }    }}

 

.Net基礎加強11

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.