Delegate is a type in c #. It is actually a class that can hold a reference to a method. Unlike other classes, the delegate class can have a signature and can only hold references to methods that match its signature. Its functions are very similar to function pointers in c/c ++. It allows you to pass the method m of Class a to the object of Class B, so that the object of Class B can call this method m. But compared with function pointers, delegate has many advantages that function pointers do not possess. First, the function pointer can only point to static functions, while delegate can reference both static functions and non-static member functions. When referencing a non-static member function, delegate not only saves the reference to this function entry pointer, but also saves the reference to the class instance that calls this function. Second, compared with function pointers, delegate is an object-oriented, secure, and reliable managed object. That is to say, the runtime can ensure that the delegate points to a valid method. You do not need to worry that the delegate will point to an invalid or out-of-bounds address.
The delegate in. net is similar to the function pointer in c or c ++. Delegate allows programmers to encapsulate method references in the delegate object. The delegate object can then be passed to the code that can call the referenced method, without knowing which method will be called during compilation. Unlike function pointers in c or c ++, delegation is object-oriented, type-safe, and secure. A delegate Declaration defines a type, which uses a specific set of parameters and the return type encapsulation method. For static methods, the delegate object encapsulates the methods to be called. For instance methods, the delegate object encapsulates an instance and a method on the instance at the same time. If you have a delegate object and a set of appropriate parameters, you can use these parameters to call the delegate. An interesting and useful attribute of delegation is the class of the object that it does not know or care about. All objects are allowed. Only the parameter type and return type of the method must match the parameter type and return type of the delegate. This makes the delegate fully suitable for "anonymous" calls.
[<Attrlist>] [accessmodifier] _ [shadows] delegate [sub | function] name [(of typeparamlist)] [([parameterlist])] [as type]
It is easy to implement a delegate. You can use the following three steps to implement a delegate:
1. Declare a delegate object. It should have the same parameter and return value type as the method you want to pass.
2. Create a delegate object and pass in the functions you want to pass as parameters.
3. Use the object created in the previous step to call the method where an asynchronous call is to be implemented.
The following is a simple example:
Solve with delegate (asynchronous call ):
Public void setscore (int value)
{
If (value> 100 | value <0)
{
Console. out. writeline ("Incorrect score ");
}
Else
{
Score = value;
If (advisedelegateinstance! = Null)
{
Advisedelegateinstance. begininvoke (score, null, null );
}
}
}
However, we lost the instructor's return result and did not know whether we had passed the course.
Use the round message method to obtain the result:
Public void setscore (int value)
{
If (value> 100 | value <0)
{
Console. out. writeline ("Incorrect score ");
}
Else
{
Score = value;
If (advisedelegateinstance! = Null)
{
Iasyncresult res = advisedelegateinstance. begininvoke (score, null, null );
While (! Res. iscompleted) system. threading. thread. sleep (1 );
String result = advisedelegateinstance. endinvoke (res );
Console. out. writeline ("the student receives the result t returned by the teacher" + result );
}
}
}
1 2