Learn c#-entrust every day

Source: Internet
Author: User
Tags define connect
As for C + +, delegate is the equivalent of a function pointer, looking at the following code:

Class Person

{

Public person (string name)

{

This.name=name;

}

Some properties and methods

String name;

public void Eat (Food Food);

}

The person class has a eat method, and for its instance, John, Dick, Harry have their own Eat method

Person Zhansan,lisi,wangwu;

Zhansan=new person ("John");

Lisi=new person ("Dick");

Wangwu=new person ("Harry");

We can call the Eat method individually by defining a delegate

public void Delegate Eat (Food Food);

If you want to call Zhansan.eat (food)

Eat zhansaneat=new Eat (zhansan.eat);

Other similar:

Eat lisieat=new Eat (lisi.eat);

Eat wangwu=new Eat (wangwu.eat);

This invocation of zhansaneat (food) is equivalent to calling zhansan.eat (food)

Commissioned the most useful is the chain, if John, Dick, Harry Dine together

You can define the delegate as follows

To define a composite delegate

Eat togethereat;

In C #, connect the method to the delegate by "+", and the delegate is added to the delegate chain

Remove a method from the delegate chain by "-"

Togethereat=zhansaneat+lisieat+wangwueat;

John, Dick, Harry Eat watermelon together.

Togethereat (watermelon);

Not with John, only Dick and Harry Eat.

Togethereat=lisieat+wangwueat;

Togethereat (watermelon);

The event mechanisms in. NET are implemented by delegates.

The following is a section of the C # technology disclosure of the source code, I added some comments to explain the implementation of the event mechanism


The Inventorymanager class is used to update inventory, and it also defines the event that should be triggered when inventory is updated, that is, it publishes a delegate called by the Subscriber
The InventoryWatcher class defines subscribers and can choose whether to add themselves to the publisher's list to be notified when inventory is updated
Using System;

Namespace Delegateevents
{
A derived class that defines EventArgs to carry event information
Class InventoryChangeEventArgs:System.EventArgs
{
Public Inventorychangeeventargs (String sku,int change)
{
This.sku=sku;
this.change= change;
}
string sku;
public string Sku
{
Get{return SKU;}
}
int change;
public int Change
{
Get{return Change;}
}
}
Published by
Class Inventorymanager
{
Declare a delegate, two parameters are necessary, the first is the Publisher object, the second must be EventArgs class or its derived class
public delegate void Inventorychangeeventhandler (Object Source,inventorychangeeventargs e);
Define the event instance for the delegate (that is, the delegate chain, which is used by the subscriber to add itself to the delegate chain
public event Inventorychangeeventhandler Oninventorychangehandler;
Inventorymanager Method for updating inventory
public void Updateinventory (string sku,int change)
{
if (0==change)
Return
Defining event parameter instances, passing SKU and change information
Inventorychangeeventargs e=new Inventorychangeeventargs (sku,change);
To determine if the delegate list is empty, and if not, a subscriber subscription
if (this. Oninventorychangehandler!=null)
{
Console.WriteLine ("[Inventorymanager.updateinventory] raising event to all subscribers...\n");
Call the method on the delegate list in turn
This. Oninventorychangehandler (this,e);
}
}
}
Subscribed by
Class InventoryWatcher
{
Define Publisher
Inventorymanager Invnetorymanager;
Public InventoryWatcher (Inventorymanager Inventorymanager)
{
Console.WriteLine ("[Inventorywatcher.inventorywatcher] subscribing to Inventorychange event\n");
This.invnetorymanager=inventorymanager;
Connect yourself to Inventorymanager.inventorychangeeventhandler delegate
Inventorymanager.oninventorychangehandler+=new Inventorymanager.inventorychangeeventhandler (OnInventoryChange);
}
The subscriber's method, which is invoked when the Publisher updates inventory
void Oninventorychange (Object Source,inventorychangeeventargs e)
{
int change=e.change;
Console.WriteLine ([Inventorymanager.oninventorychange]\n\tpart ' {0} ' is {1} by {2} units\n ', e.sku,change>0?] Increased ":" Decreased ", Math.Abs (E.change));
}
}
<summary>
Implementation of event mechanism examples with delegates
</summary>
Class Delegateevents
{
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
To define a delegate Publisher object
Inventorymanager inventorymanager=new Inventorymanager ();

Console.WriteLine ("[Delegateevents.main instantiating subscriber object\n");
Define subscriber objects
InventoryWatcher inventwatcher=new InventoryWatcher (Inventorymanager);
Inventorymanager.updateinventory ("111 006 116", 2);
Inventorymanager.updateinventory ("111 005 383", 5);

Console.ReadLine ();
}
}
}

The above is only a little understanding of their own, many places are immature, may cite examples are not appropriate, welcome to guide, and we learn together





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.