c#第五節課

來源:互聯網
上載者:User

標籤:

首先是關於這個keyword

delegates

delegates我的理解是一個指向函數的指標,我們要預先聲明這個指標可以指向的函數的簽名。代碼如下

delegate int Sample( int x, int y );

這裡聲明了一個sample是有兩個參數的函數的指標

class Class2{      public int method( int x, int y )      {       return x*y;      }}class Class1{     static void Main(string[] args)     {       Class2 instance = new Class2();       Sample mySample               = new Sample(instance.method);       int result = mySample( 2, 3 );       Console.WriteLine( result );     }}

然後我們在使用的時候可以如上,mysample就是一個指向class2中method的函數,當然對應的是執行個體化後的Class2也就是instance的指標

那麼既然已經有了一個函數名我們為什麼要多此一舉再造一個委託(其實我覺得這個翻譯還不如直接說函數指標來的明白)呢?

來說一下這個委託有哪些妙用吧

我們來看下面這個例子

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{    delegate void Sample(int x,int y);    class sizeyunsuan    {        public void mult(int x,int y)        {            Console.WriteLine("相乘的結果是{0}", x * y);        }        public void plus(int x,int y)        {            Console.WriteLine("相加的結果是{0}", x + y);        }        public void minus(int x, int y)        {            Console.WriteLine("相減的結果是{0}", x - y);        }        public void divide(int x, int y)        {            if ( y == 0 )            {                Console.WriteLine("0不能當做除數");                return;            }            int p = gcd(x, y);            x = x / p;            y = y / p;            if (y != 1)                Console.WriteLine("相除的結果是{0}/{1}", x , y);            else                 Console.WriteLine("相除的結果是{0}", x);        }        private int gcd(int x, int y)        {            if (x == 0) return y;            return gcd(y % x, x);        }    }    class Program    {        static void Main(string[] args)        {            sizeyunsuan test = new sizeyunsuan();            Sample nowSample;            nowSample  = test.plus;            nowSample += test.minus;            nowSample += test.mult;            nowSample += test.divide;            nowSample(8, 3);            nowSample(8, 4);            Console.ReadLine();        }    }}

這裡面我們的nowSample就是一個函數指標,對應四則運算

先貼結果

這裡我們完成了多個函數的大一統哈哈哈哈哈哈

其實就是我們對於同類的函數,都要調用的話,可以把它們一個一個的加入到這個指標上面,只要是同類(簽名一樣)

比如這裡我的四則運算就是對於給定的xy都要進行加減乘除運算,那麼我們就可以這樣寫,當然在我這個例子裡面還體現不出來,但是真的寫應用的時候很多時候我們很多東西要做的都是一樣的工作,而把多個類似的函數進行一個一個調用至少我們也要把每個函數簽名寫一遍不是,而這樣加到這個指標後面就只要一遍以後再用的時候就可以直接用啦

 

之後還提及了async和await關鍵字的使用,樣本

public async Task<int> ExampleMethodAsync() {   var httpClient = new HttpClient();   int exampleInt = (await httpClient.GetStringAsync("http://msdn.microsoft.com")).Length;    ResultsTextBox.Text += "Preparing to finish ExampleMethodAsync.\n";    return exampleInt; } 

因為我並沒有太明白如何使用之後查閱了一些網上的文章,大概瞭解了async可以將程式變為非同步執行,而await則可以讓單線程的某處堵塞如果停住了不用所有的都等待這一步的堵塞,可以非同步就不再等待(不知道對不對

https://msdn.microsoft.com/zh-cn/library/hh191443.aspx

這裡有詳細的介紹,看了一遍大概理解了一點,準備下次上課之前再看一遍(留做記錄)

c#第五節課

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.