I. Start with regret
Sorry for your best Programming Language C #, because I have only understood how to use delegate and event in C # so far. Well, let's talk about the simple use of Delegate and event on a dark night today, hoping to help beginners. PS: Don't learn C # for a few years as I did. In the end, I don't know how to use delegate and event.
2. What is delegate?
Children, I have always learned C language. If you haven't learned as well as me, I have always used function pointers. Even if you have never heard of them, I am so bold to tell you, you can fully understand delegate as a function pointer in C. It allows you to pass the M method of Class A to the object of Class B, so that the object of Class B can call this method M, to put it bluntly, the method can be passed as a parameter. However, delegate is a little different from 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. 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, in comparison 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.
What can be better explained than an example? The code is the final principle. Let's take a look at several examples:
Example 1:
| 123456789101112131415161718 |
public class DelegateTest{ // Declare the delegate object public delegate void CompareDelegate(int a,int b); // The method to be passed. It has the same parameter and return value type as comparedelegate. public static void Compare(int a,int b) { Console.WriteLine((a>b).ToString()); } public static void Main() { // Create a delegate object CompareDelegate cd = new CompareDelegate(DelegateTest.Compare); // Call delegate cd(1,2); }} |
Another example:
| 12345678910111213141516171819 |
public delegate void MyTestDelegate(int i);public class Program{ public static void Main() { // Create a delegate ReceiveDelegateArgsFunc(new MyTestDelegate(DelegateFunction)); } // This method receives a delegate parameter, that is, a function is received as a parameter. public static void ReceiveDelegateArgsFunc(MyTestDelegate func) { func(21); } // Method to be passed public static void DelegateFunction(int i) { System.Console.WriteLine("The passed parameter is: {0 }.", i); }} |
Well, with your IQ, You should have understood what is going on with the delegate commission. If you still don't know what it is, you can play the right hand 2. Next let's talk about the event.
3. Events let you understand how the silly onclick came from
Okay, I admit it to us. net programmers are very dumb, drag controls, and then onclick to complete, it can only blame Microsoft for doing too well, so that those jealous and envious Java programmers despise us. net programmer. In fact, I would like to say that our onclick is actually not easy. If we can really understand the mechanism behind it, then we. net programmers are more confident to face the Java programmers who despise us. Today I am going to reveal the story behind onclick.
When talking about onclick, you have to talk about Event Events in. net.
In C #, event processing is actually a delegate with a special signature, as shown below:
Public Delegate void myeventhandler (Object sender, myeventargs E );
The two parameters, sender represents the event sender, and E is the event parameter class. The myeventargs class is used to contain event-related data. All event parameter classes must be derived from the system. eventargs class. Of course, if your event does not contain parameters, you can directly use the system. eventargs class as the parameter.
Let's take onclick as an example to talk about the implementation of the event.
| 12345678910111213141516171819202122232425262728293031323334353637383940 |
// Define an eventargs here, because I want to know the clickerpublic class ButtonClickArgs : EventArgs{ public string Clicker;} public class MyButton{ // Define a delegate public delegate void ClickHandler(object sender, ButtonClickArgs e); // Define the event type as the clickhandler delegate defined above public event ClickHandler OnClick; public void Click() { // N operations may be performed before... //..... // At this time, the click event is triggered, and the input parameter clicker is the main Ivy. OnClick(this, new ButtonClickArgs() { Clicker = "ivy" }); }} public class Program{ public static void Main() { MyButton btn = new MyButton(); // Register the event and press the btn_onclick method into the event queue, // You Can + = multiple. Click one here. btn.OnClick += new MyButton.ClickHandler(btn_OnClick); } // You are familiar with this function. It is the code automatically generated by double-clicking the button. public static void btn_OnClick(object sender, ButtonClickArgs e) { Console.WriteLine("Really cheap, I was clicked by Ivy! "); }} |
Well, I think you should be able to understand event after reading this example. If you don't know how to beat 2 in the right hand, whether you know it or not, I understand it anyway.
Iv. Summary
This time I only talked about delegate and event, but it is very basic. I hope you can understand it. Don't be bullied by Java Programmers. Please improve it. Don't always double-click the button.
BTN. onclick + = new mybutton. clickhandler (btn_onclick );
It seems that you are a professional, so that those Java programmers understand that cool onclick is also a level!
Good night, everyone. Have a good dream!
From: http://www.itivy.com/ivy/archive/2011/8/5/csharp-delegate-and-event.html