Resolve agents and events in C # (1)

Source: Internet
Author: User
Resolve agents and events in C # (1)

Suddenly I wrote this article. It's really a bit of a work-related feeling. I think it's nothing, it's hard

In fact, many people have written what they understand, and I promise it is at least no worse than what I write.

I still think it is necessary to propose it because it is interesting to correctly understand the proxy and events.

It is necessary. Well, let's talk about the relationship between them. Of course, there will be some examples.

First, I want to talk about events. events, as the name suggests, are messages received by windows.

Let me give you a few examples of related events, such as moving the mouse, pressing, and so on. What about proxy?

Many people say that it looks like a hosted function pointer.

This is true. On msdn, we often see the word "multi-channel broadcast" about proxy.

I think it is good, but it is hard to understand, but I will explain it in detail below.

Proxy: (in some books, it is also translated as a reference or delegate. In English, this is the "delegate ")

I think many people who are new to C # will be interested in agents. In fact, this is also true. If you don't know it, you won't be able

In the traditional sense, the proxy is a type in C #.

It seems to be safer and more in line with the OO spirit. What proxy actually does is to wrap the function by referencing it.

And make the function have a valid return value. I don't know if I can understand it. So I will give you an example to build a house.

Obviously, I am talking about what you do. Building a house is a proxy. It refers to what you want to do, but it does

I didn't do anything. In fact, when you did the house building job, What was the result? Of course, it is the establishment

A house. Yes, building a house is a proxy, and how to build a house is what the function should do.

Is the return value. Remember, I once said, Is proxy a type? Well, I think you should remember, because,

That's novel. At least I thought so. Okay. Let's take a look at the namespace system. delagate. Have you seen it? That

Proxy class.

For example:

Public Delegate void getstring () // I declare a proxy

Now I want to use it as follows;

Int I = 100;

Getstring GS = new getstring (I. tostring); // here I want to use the tostring method of int

Entered in a proxy. It seems like you want to construct a function. This is what we often see in the book "name is equivalent, and

It is not equivalent to the structure. "I want to see that you still don't understand it. Then, I will use another Proxy.

As follows:

Float J = 0.0001;

Getstring GS = new getstring (J. tostring); // you can see the tostring method of Int.

The structure of the tostring method is different from that of float, but the return values of the name and type are

The parameters are the same. Now, I think you should understand it.

However, we often see such sentences in msdn. Single-channel proxy and multi-channel broadcast. It looks a little hard to understand.

In fact, it is a headache for me to start reading such a sentence. So, I think the example is the best way to explain it.

Single delegate: (single-channel proxy)

Literally, we can understand that this proxy only acts as a proxy for a function. Well, let's

Let's take a look at how it works. Next I will define a proxy like this:

Public Delegate bool myfun (string STR, int I)

Now let's write another method as follows:

Bool comparestrtoint (string S, int I)

<

If (S. compareto (I. tostring () = 0)

Return true;

Else

Return false;

>

This method is easy to do, right? It's just a comparison of characters. What is the relationship with the proxy? Remember

What do I say? The agent is translating the verb into nouns. The Code is as follows:

Myfun mf = new (comparestrtoint );

String S = "10000 ";

Int I = 10000;

Console. writeline ("value =" + Mf (S, I ));

Output result:

Value = true

This is a single-channel proxy. It only acts as a proxy. Well, maybe you want to look at complicated examples. What's more interesting,

This is the time to discuss multicast.

Multi-channel broadcast:

A proxy acts as a proxy at the same time. As we mentioned earlier, you need to build a house.

Building houses, building gardens, and other buildings. However, they all build houses and pass the same parameters.

The returned values are of the same type as houses. So why don't we look for an agent to complete such a task? Set

These things are done by him alone, which does not save us a lot of time and money. Yes, we can do that.

System. multicastdelegate you can also find this class in. NET Framework, multi-channel proxy

It is translated into multi-channel broadcast on msdn. In fact, it also loads the operator + =. In fact, multi-channel broadcast and single-channel proxy are in use.

The difference is not big. You can refer to the following example.

Using system;

Namespace multi_castdelegate

<

/// <Summary>

/// Summary description for class1.

/// </Summary>

Class myclassdelegate

<

/// <Summary>

/// The main entry point for the application.

/// </Summary>

Public Delegate string intdelegate (string S );

>

>

Using system;

Namespace multi_castdelegate

<

/// <Summary>

/// Summary description for myimplementingclass.

/// </Summary>

Public class myclass

<

Public myclass ()

<

>

Public static string writestring (string S)

<

Console. writeline ("Writing string ");

Return "null ";

>

Public static string logstring (string S)

<

Console. writeline ("loging string ");

Return "null ";

>

Public static string transmitstring (string S)

<

Console. writeline ("transmitting string ");

Return "null ";

>

>

>

The main class:

Using system;

Using system. Threading;

Namespace multi_castdelegate

<

/// <Summary>

/// Summary description for test.

/// </Summary>

Public class test

<

Public static void main ()

<

Myclassdelegate. stringdelegate

Writer, logger, transmitter;

Myclassdelegate. stringdelegate

Mydelegate;

Writer = new

Myclassdelegate. stringdelegate (myclass. writestring );

/// Calling writer

Writer ("Hello I am writer just acting like single cast ");

Logger = new myclassdelegate. stringdelegate (myclass. logstring );

/// Calling Logger

Logger ("Hello I am logger just acting like single-cast ");

Transmitter = new myclassdelegate. stringdelegate (myclass. transmitstring );

/// Calling Transmitter

Transmitter ("Hello I AM transmitter just acting like single-cast ");

/// Here mydelegate used the combine method of system. multicastdelegate

/// And the delegates combine

Mydelegate = (myclassdelegate. stringdelegate) system. Delegate. Combine (writer, logger );

Mydelegate ("used combine ");

/// Here transmitter is also added using the overloaded form of Combine

Mydelegate + = transmitter;

Mydelegate ("using overloaded form ");

/// Now using the Remove Method

Mydelegate = (myclassdelegate. stringdelegate) system. Delegate. Remove (mydelegate, writer );

Mydelegate ("without writer ");

/// Overloaded remove

Mydelegate-= transmitter;

Mydelegate ("without transmitter ");

System. Threading. thread. Sleep (2300 );

>

>

>

(The above example was found on a foreign website. If you think it is good, you can apply it directly .)

The above example focuses on the two overloaded operators. "-=" and "+ =". Through the above example, you can clearly understand

You can see how multi-channel broadcasting proxy methods at a time. Of course, you can also delete operations that you don't want to use "-= ".

.(

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.