the delegate in c shap
来源:互联网 发布:java 泛型 getclass 编辑:程序博客网 时间:2024/05/15 05:21
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!
- the delegate in c shap
- C#中的Delegate机制(Delegate in C#)
- shap
- 汉字转换字符C#(c shap) 代码
- The relation between event and delegate in visual c#
- Implementing a delegate chain system in Objective-C
- C#-delegate
- The File Functions in C
- Shap圆角
- C#-委托(delegate)
- C#-委托(delegate)
- delegate,C#,代理
- [C#] 委托delegate
- objective-c delegate
- objective-c - delegate
- Objective-C delegate理解
- objective-c - delegate
- objective-c delegate
- EXT combobox赋值
- WdatePicker.js的使用方法 帮助文档 使用说明(时间控件)
- sql注入:
- 订单数量超过forecast数量问题
- MySQL查询获取行号rownum
- the delegate in c shap
- android 按返回退出应用
- 遗传算法入门
- C# 串行化与反串行化--使用BinaryFormatter进行串行化
- hdu 1261 (大数除法)
- 泛泰A850工程机与正式机分区表对比
- Dialog七种模式详解
- 手机客户端和服务器通信时如何安全高效的进行身份验证?
- 类型字符转换