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!