C # Delegate,

Source: Internet
Author: User

C # Delegate,

Preface

I wrote a technical blog for the first time. Since I am also learning C #, I have no experience. I just encountered this problem. I checked others' explanations and summarized some content based on my own understanding. I hope you can give me some advice on the poorly written or wrong content. Thank you.

Summary

There are several question marks: What is delegation? What is the delegated length (how to use it )? When do I use delegation? Turning these question marks into periods is Blog content. Let's get into the topic together! Let's Go

Subject

What is delegation?

On msdn, the delegate is a class that can save reference to the method and has a signature. You can only reference methods that match your signature. It is equivalent to a pointer or callback of A type security function. After reading the explanation, do you have any questions: What is the so-called signature? In fact, the so-called signature is to sign your own name, and the name is used to differentiate. Since the delegate is a reference to the method, the condition for distinguishing the method should be the condition for distinguishing the delegate, the "signature" of the delegate should be the function return value and form parameter. Do you understand it a little bit?

What is the delegated length?

As mentioned above, the delegate is a class, so the delegate must be instantiated. First, declare a delegate type:

delegate void myDelegate(string text);

Here, delegate is the keyword, myDelegate is the custom delegate type name, void and string text are the delegate signature.
Declare another delegate for this delegate class:

myDelegate Update;

Here, myDelegate is the delegate type stated earlier, and Update is the custom delegate name.
Finally, there are two ways to instantiate the delegate:

The first type is the same as the general instantiation class.

Update = new myDelegate(UpdateUI);

Among them, UpdateUI is the same signature function as the delegate, that is, void UpdateUI (string ).

Method 2: Registration Method

Update +=UpdateUI;

After all the preparations are completed, you can use the delegation with confidence. Use delegate to pass parameters:
 

Update(str);

Str is a string variable.

When should I use it?

I wonder if you have encountered the following error when using multi-threaded programming:

This is like a situation where someone else is busy doing his own thing. You suddenly ask him for something. If you interrupt someone else, will someone else be happy. Then you tell him in advance that you will come here and entrust it to him first. The results will certainly be different. Therefore, programming is the same as being a human. When a thread is using a variable, the other thread cannot access it. This is why we need to delegate this thread to notify another thread of what I need.

In addition, we should all know the observer mode. It doesn't matter if you don't know. I can tell you what I know. For example, let's take an example of what I saw on another blog. Suppose there is a hot pot, when the temperature reaches 90 degrees, the alarm will sound, the temperature value should also be displayed on the display screen, and so on, there can be a variety of reactions. If all these operations are performed by humans, then a person is an observer and a hot water bottle is an observer. When a person sees the temperature change to 90 degrees, an alarm should be triggered, display temperature and so on, busy, and fast to complete. As a programmer, do you really want to relieve the burden on him.

According to this model, you must have come up with a clever method. Is that true? Create an abstract observer class as an interface and add it to the heater class of the hot pot. When the temperature reaches 90 degrees, broadcast it through the observer. This implementation method is actually the observer mode. You can imagine that you have fully learned how to use the observer design mode.

In fact, C # already has a ready-made class that can act as an observer. You don't have to create it yourself, which is troublesome. You must have guessed it again. This class must be the delegate class. Yes, it is it, it is it.

Class Heater {public delegate void myDelegate (int tempreture); // declare the delegate public event myDelegate Message; // declare the event public int tempreture; public void boild () {for (int I; I <101; I ++) {this. tempreture = I; if (tempreture> 90)
{
If (Message! = 0) // can be used only when an object is registered
{
Message (tempreture); // call the registered method}
}
}}}
Class Alarm
{
Public void alarm (int tempreture)
{
Console. WriteLine ("tick, the water temperature is {0. ", Tempreture );
}
}
Class Program {
Static void Main ()

{
Heater heater = new Heater ();

Alarm alarm = new Alarm ();

Heater. Message + = alarm. alarm; // Registration Method

Heater. Message + = (new Alarm (). alarm; // Method for registering an anonymous object

Heater. boild ();
}
}

Summary
In my understanding, when encapsulating a class, I want to upload the data generated in one class to another class or generate it in one thread, at the same time, you can use the delegate when using it in another thread. If you have not understood it, you can click the link below to continue learning. I also learned something from this blog post:

Http://www.cnblogs.com/jimmyzhang/archive/2007/09/23/903360.html

I reviewed it again in the morning and drew a picture of how to implement the observer design pattern through event delegation. It's a bit ugly, but the process should be well understood. If you are not clear about it later.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.