Delegate
来源:互联网 发布:热血战歌羽化数据 编辑:程序博客网 时间:2024/05/16 06:42
- A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, delegates are object-oriented, type safe, and secure. The type of a delegate is defined by the name of the delegate.
- Delegate types are derived from the Delegate class in the .NET Framework. Delegate types are sealed—they cannot be derived from— and it is not possible to derive custom classes from Delegate. The Delegate class is the base class for delegate types.However, only the system and compilers can derive explicitly from the Delegate class or from theMulticastDelegate class.It is also not permissible to derive a new type from a delegate type.The Delegate class is not considered a delegate type; it is a class used to derive delegate types. Most languages implement a delegate keyword, and compilers for those languages are able to derive from the MulticastDelegate class; therefore, users should use the delegate keyword provided by the language.
- Unlike other classes, a delegate class has a signature, and it can hold references only to methods that match its signature. A delegate is thus equivalent to a type-safe function pointer or a callback.
- Because the instantiated delegate is an object, it can be passed as a parameter, or assigned to a property. This allows a method to accept a delegate as a parameter, and call the delegate at some later time. This is known as an asynchronous callback, and is a common method of notifying a caller when a long process has completed. When a delegate is used in this fashion, the code using the delegate does not need any knowledge of the implementation of the method being used. The functionality is similar to the encapsulation interfaces provide.
- Delegates with more than one method in their invocation list derive from MulticastDelegate, which is a subclass of System.Delegate.
- The common language runtime provides each delegate type with BeginInvoke and EndInvoke methods, to enable asynchronous invocation of the delegate.
- We can use named methods, anonymous methods or lambda expression to instantiate the delegate:
- Named method
delegate void Del();
void DoWork(); // support both instance method and static method
Del d = DoWork; // Is equal to "Del d = new Del(DoWork);", but simpler.
- Anonymous method
delegate void Del();
Del d = delegate() {/*....code block...*/};
delegate int Del(int x);
- Lambda expression (a type of anonymous method)
Del d = (x)=> {return x * x;}
delegate void Del();
- Pass delegate as a parameter
void TestDel(Del callback)
{
callback(); // calling delegate
}
TestDel(DelImpl); // Instantiating delegate
static void DelImpl()
{
// Implementation.
}
- Multicast delegate: (+: add object to delegate, -:remove object from delegate, execute in order, only can combine the delegate with same type), for example:
delegate void Del();
void Hello();
void World();
Del d1 = Hello;
Del d2 = World;
Del d3 = d1 + d2;
Del d4 = d3 - d2;
d3(); // first call Hello(), then call World();
d4(); // only call Hello();
- Typical use scenario: If lower layer (DB) wants to callback higher layer (UI),we can define a delegate in lower layer,then in higher layer we instantiate that delegate with the implementation method in higher layer,for example,
lower layer: public delegate void MyDelegate();
public static MyDelegate MyDel= null;
{
if (MyDel != null)
MyDel();
}
higher layer:lower.MyDel = DelImpl;
void DelImpl()
{
//implementation
}
- Lambda expressions are not limited to LINQ queries. You can use them anywhere a delegate value is expected, that is, wherever an anonymous method can be used.
- delegate
- delegate
- Delegate
- delegate
- Delegate
- Delegate
- Delegate
- Delegate
- delegate
- Delegate
- Delegate
- Delegate
- Delegate
- delegate
- Delegate
- delegate
- delegate
- delegate
- windows驱动与用户态交互时候,用户无法打开驱动的问题
- 使用busybox
- 微博活动已沦为鸡肋
- sqlserver 查询多个字段数据重复sql语句
- C#专题之C#的变量
- Delegate
- window 下删除svn
- FreeCMS有些功能出现乱码怎么办?
- 进程间通信:用信号量实现对共享内存读写顺序的控制
- cocos2d-x初学笔记07:角色动作Action
- 使用哪种压缩格式
- JAVA_OPTS参数-Xms和-Xmx的作用
- js实现查看大图
- Linux 中如何安装卸载软件【转】