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 DelegateThe 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...*/};

  • Lambda expression (a type of anonymous method)
delegate int Del(int x);

Del d = (x)=> {return x * x;}

  • Pass delegate as a parameter
delegate void Del();

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;
  void test()
{
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.
原创粉丝点击