Unlike other classes, a delegate class can have a signature, and it can only save references to methods that match its signature.
A delegate declaration defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static method or an instance method. Delegates are roughly similar to function pointers in C++; In any case, delegates are type-safe. The above definition explains that the delegate declaration defines a reference type, which is used to compress the specified form of method. Instances of delegates compress static methods or instance methods. Delegates in C++ are very similar to function pointers. However, the difference is that delegates are safe and type-safe.
Found it on the codeProject website.
C# delegate is a callback function. In other words, delegation is a way to provide feedback from the class server to the class client.
The delegate of C# is a callback function. In other words, delegation is a way to provide feedback from the server side of the class to the client side of the class. C# delegate is smarter than "standard" callbacks because it allows you to define a strict parameter list that is passed from the class server to the class client.
But C# delegate is smarter than ordinary callback functions. Because it allows a strict parameter list to be defined. This parameter list contains parameters passed from the server of the class to the client of the class. Delegate type of C#
Delegate is a function pointer, but compared with ordinary function pointers, there are three main differences: 1) A delegate object can carry multiple methods at a time instead of one at a time. When we call a delegate loaded with multiple methods, all the methods are called in the order of loading on the delegate object-we will see how to do this later. 2) The methods carried by the delegate object do not need to belong to the same category. All methods carried by the delegate object must have the same prototype and form. However, these methods can be static and non-static, and can be composed of one or more members of different categories. 3) The declaration of the delegate type essentially creates a new subtype instance, which is derived from the. NET library framework. They provide a set of public methods to query the delegate object or the methods it carries.
-
The function it realizes is very similar to the function pointer in C/C++.
It allows you to pass the method M of class A to another 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 have. First of all, function pointers can only point to static functions, while delegate can refer to both static functions and non-static member functions.
When referring to a non-static member function, delegate saves not only a reference to the entry pointer of the function, but also a reference to the class instance calling the function. Secondly, compared with function pointers, delegates are object-oriented, type-safe and reliable managed objects.
In other words, the runtime can ensure that delegate points to an effective method, and you don't have to worry that delegate will point to an invalid address or an out-of-bounds address.
Implementing a delegate is very simple. It can be achieved through the following three steps: 1. Declare a delegate object that should have the same parameters and return value types as the method to be passed.
2. Create a delegate object and pass in the function to be passed as a parameter.
3. Call the method through the object created in the previous step, in which asynchronous calling should be realized. Use the system;
Public class MyDelegateTest
{
//Step 1, declare the delegate object.
Public delegate void MyDelegate (string name);
//This is the method we want to pass, and its parameters and return value types are the same as MyDelegate.
Public static void MyDelegateFunc (string name)
{
Console. WriteLine ("Hello", name);
}
Public static void Main ()
{
//Step 2, create a delegate object.
MyDelegate md = new MyDelegate(MyDelegateTest. MyDelegateFunc);
//Step 3, call delegate.
MD(" Sam 1 1 1 1 ");
}
The output result is: Hello, Sam1111.
Understand the delegate, let's take a look at how C# handles events. The event handling in C# is actually a delegate with a special signature, like this: public delegate void my eventhandler (object sender, my eventargese); Two parameters, sender, represent the event sender, and e is the event parameter class. The MyEventArgs class is used to contain data related to events, and all event parameter classes must be derived from the system. EventArgs class. Of course, if your event contains no parameters, you can use the system directly. The EventArgs class is used as a parameter. It's that simple. Combined with the implementation of delegate, we can boil down the implementation of custom events to the following steps: 1. Defines the delegate object type, which has two parameters, the first parameter is the event sender object, and the second parameter is the event parameter class object.
2. Define the event parameter class, which should be derived from the system. EventArgs class. This step can be omitted if the event has no parameters.
3. Define the event handling method, which should have the same parameters and return value types as the delegate object.
4. Use the event keyword to define an event object which is also a delegate object.
5. Use the+= operator to add events to the event queue (the-= operator can delete events from the queue).
6. Write the event triggering method by calling delegate where the event needs to be triggered. Generally speaking, this method should be a protected access restriction, which can neither be called in public mode nor be inherited by subclasses. The name is OnEventName.
7. Call the event trigger method to trigger the event at the appropriate place. Here is a simple example:
Use the system;
Public class EventTest
{
//Step 1, define the delegate object.
Public delegate void MyEventHandler (object sender, system. EventArgs e);
//omit step 2
Public class MyEventCls
{
//Step 3, define the event handling method, which has the same parameters and return value class//type as the delegate object.
Public void MyEventFunc (object sender, system. EventArgs e)
{
Console. WriteLine ("My activities are fine!" );
}
}
//Step 4, define the event object with the event keyword.
Private event MyEventHandler myevent
Private MyEventCls myecls
Public Event Test ()
{
my ecls = new MyEventCls();
//Step 5, add the event to the queue with the+= operator.
This.myevent += new MyEventHandler(myecls. MyEventFunc);
}
//Step 6, write an event trigger function by calling delegate.
Protected void OnMyEvent(System. EventArgs e)
{
if(myevent! = empty)
myevent(this,e);
}
public void RaiseEvent()
{
EventArgs e = new EventArgs();
//Step 7, trigger the event.
on myevent(e);
}
Public static void Main ()
{
event test et = new event test();
Console. Write ("Please enter'' a'');
String s = console. ReadLine();
if(s == "a ")
{
et。 RaiseEvent();
}
other
{
Console. WriteLine(" Error ");
}
}
}