the delegate in c shap

來源:互聯網
上載者:User
    I haven't write the article which related to programming long time.Today I found a interesting element in C sharp-------the delegate.    the delegate in C sharp is similar to the function pointer in C/C plus plus.    delegate is a type that make the reference to the function.maybe it sounds puzzled,but it's mechanism look like simple.it similar to the function very much but it hasn't function body.and you must use the key words "delegate".the definition ofdelegate must point out a return type and a paramters list;    when the delegate has been defined,you can define a variable of delegate.then initialize the delegate with the  similar return typeand parameters list like the function you are going to use.the last step is use the delegate to call the function,the function name was replaced by delegate name;I will give a example below.the following code gvie a function to simulate  console writeline.the code
using System;using System.Collections.Generic;namespace MyClass{struct Student{public string name;public double id;public void putInformation(){Console.WriteLine("name:{0}",name);Console.WriteLine("id:{0}",id);}}class Test{delegate double ProcessDelegate(double param1,double param2);delegate string GetlineDelegate();static double Multiply(double param1,double param2){return param1*param2;}static double Divide(double param1,double param2){return param1/param2;}static string ReadLine(){return Console.ReadLine();}static void Main(string[] args){Console.WriteLine("Please input a stirng:");GetlineDelegate getString = new GetlineDelegate(ReadLine);string myString =  getString();Console.WriteLine("Your string:{0}",myString);Console.ReadKey();}}}
we used the delegate----GetlineDelegate to call the function ReadLine() so that we can get a string from console,the feture is similar to the  Console.WriteLine();it was so amazing!how did the delegate work.step 1define you own function,such as below.static string ReadLine(){return Console.ReadLine();}
step 2     Give a declaration about the delegate acording to the function feature.           delegate string GetlineDelegate();     1 return type:the delegate return type must be same as the function.           such as:           function                 string ReadLine()           delegate                 string GetlineDelegate()           the return type is "string".                                2 params list :the delegate's params list must be same as the function'sparams list;         such as:              function                   ReadLine()  //is null              delegate                   GetlineDelegate()//also is nullstep 3      Create an delegate object by the key word "new" when you want use it      like that:      GetlineDelegate getString =new GetlineDelegate(ReadLine);      we should put the function name on the constructor parmas list----the ReadLinestep 4      Use it!      we can use the delegate as call the function, actually,give the same params the function       need.just like the following.      string myString =  getString();      we also can use like this"string myString = ReadLine()".ok,it's very easy isn't it?I just said,the delegate like little about the function pointer.because has the same method tocall the function in C/C plus plus.we called this method  function pointer      give a example about the function point in C/C plus plus
#include<iostream>#include<string>using namespace std;string ReadLine(){string temp;cout<<"please input string:"<<endl;getline(cin,temp);return temp;}int main(void){ string (*Read)() = ReadLine; string myString = Read();cout<<"Your string:"<<myString<<endl;return 0;}


the follow table tell us the reason their similaritys    

wow it's a terrible result!       

相關文章

聯繫我們

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