the delegate in c shap

    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 of
delegate 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 type
and 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 1

define 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 null
step 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 ReadLine

step 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 to
call 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!
       


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值