In other words,. Net events and Delegation

Source: Internet
Author: User

I have long heard about the concept of delegation when I was learning C #. I have read a lot.ArticleI couldn't understand its meaning. Many of them explained this in the garden: "The delegate is equivalent to the C ++ function pointer ..."
I have never learned C ++. I only vaguely remember that when I was learning C language basics, I understood the pointer like this. The pointer is a variable that does not store values, it stores the memory address of another variable. So I think of value type variables and reference type variables.
Even more confused, I gradually understood its meaning in my later work and study.

Problem:
1. What is delegated.
2. When can I use the delegate.

Statement: here, we will not discuss the principle and internal mechanism of delegation... I just want to explain my understanding of delegation... the content is for reference only. You are welcome to change it to a previous version.
The following uses vernacular and a very white example to illustrate the problem.

Define a class with attributes, methods, and some methods. We can implement it.Code. Some methods need to be called to determine the functions that should be completed. Therefore, a delegate is defined here, and the specific Execution Code is implemented again during the call. In this way, different operations can be performed based on different implementation codes. It is a form of polymorphism.


I still don't understand. Let's take a look at the "white" example below:
This is our requirement.
A hotel owner asked the shopping clerk to buy cabbage and potatoes, and finally calculated the total price.
The boss said: "If the price of Chinese cabbage is more than 3.5 yuan, the Chinese cabbage impurities will be deducted. The deduction method is to remove 0.3 kilograms of impurities every 1 kg and then buy"
Shopping CLERK: "Does Tudou use this rule ?"
The boss said: "What are the rules for potatoes? You can decide whether to deduct impurities"
The shopping clerk secretly thought: "first go to the food market to see the price. If the price of potatoes is more than 2.5 yuan, the impurity will be deducted ..."
The buyer decides how to buy food.

The following code is implemented:

// Food buyer
Public class buyvegetable
{
Double cabbagemoney;
Double murphymoney;

Code
///   <Summary>
/// Buy Cabbage
///   </Summary>
///   <Param name = "price"> </param>
///   <Param name = "amount"> </param>
///   <Returns> </returns>
Public   Void Buycabbage ( Double Price, Double Amount)
{
// The boss's plan to buy Cabbage
// If the price of Chinese cabbage is less than 3, no miscellaneous will be removed. If the price of Chinese cabbage is greater than 3
If (Price <   3 )
{
Cabbagemoney = Price * Amount;
}
Else
{
Cabbagemoney = (Price -   0.3 ) * Amount;
}
}

Because you only know how to buy potatoes, you can only decide one event.
Vernacular: how to buy a potato can be decided only when it comes to the food market, but no matter how you get it, you have to buy a potato. No matter how you make a decision in the food market, you can't do without the "price" and "quantity" conditions, the delegate is used here, so this event is of the delegate type. Delegatebuymurphy (the delegate is defined below)

 

///   <Summary>
/// Potato purchase event
///   </Summary>
Public   Event Delegatebuymurphy eventbuymurphy;

 

So the method for buying potatoes is as follows:

Code
///   <Summary>
/// Buy potatoes
///   </Summary>
///   <Param name = "price"> </param>
///   <Param name = "amount"> </param>
Public   Void Buymurphy ( Double Price, Double Amount)
{
// Here, we only know how to purchase potatoes, but the specific rules can be determined at the time of purchase.
Murphymoney = Eventbuymurphy. Invoke (price, amount );
}

Calculate total amount

///   <Summary>
/// Measure the overall price
///   </Summary>
///   <Returns> </returns>
Public   Double Stat ()
{
Return Cabbagemoney + Murphymoney;
}

}
Purchase potato Commission

Code
///   <Summary>
/// Purchase potato Commission
///   </Summary>
///   <Param name = "price"> </param>
///   <Param name = "X"> </param>
///   <Returns> </returns>
Public   Delegate   Double Delegatebuymurphy ( Double Price, Double X );

 

Start purchasing with the above food-buying class

Protected void page_load (Object sender, eventargs E)
{

// Needless to say, instantiate the class.
Buyvegetable =   New Buyvegetable ();

// When purchasing cabbage, the boss's dead rule cannot be used directly.

Buyvegetable. buycabbage ( 3.6 , 8 );

 

// The buyer asked: how much is potato.
// The salesperson said: 4.8.
// The buyer said, "Wait, I will calculate how to buy it first ."
// The shopper starts to set the rule for buying potatoes. buyvegetable_eventbuymurphy (the Code is as follows)

// This rule is about buying potatoes. Of course, you have to buy potatoes.

Buyvegetable. eventbuymurphy + =   New Delegatebuymurphy (buyvegetable_eventbuymurphy );

// Purchase potatoes

Buyvegetable. buymurphy ( 4.8 , 9 );

// Calculate the total price

This . Response. Write (buyvegetable. Stat ());

}
// Method for buying potatoes buyvegetable_eventbuymurphy

Code
///   <Summary>
/// Potato purchase rules
///   </Summary>
///   <Param name = "price"> </param>
///   <Param name = "X"> </param>
///   <Returns> </returns>
Double Buyvegetable_eventbuymurphy ( Double Price, Double X)
{
If (Price >   2.5 )
{
Return (Price -   0.3 ) * X;
}
Else
{
Return Price * X;
}
}

 

 

 

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.