Multicast Assign in C# With Instance

Owlcation »

Radical »

Computing

Multicast Designate in C# With Lesson

Updated on November 29, 2018

sirama

more

I am a programmer. I suffer been running with C++, MFC, and .net technologies for 15 age. I wish television games and recital books.

1. Unveiling

Therein clause, we volition see what is a “Multicast Delegate” and how we make and use it. Multicast delegates are the combining two or more delegates of like typecast and they unitedly shape a Assign Concatenation . Apiece player in the depute concatenation should birth a empty reappearance eccentric.

In the cypher, we volition issue an model of an Rescript Processing Scheme that makes use of the Multicast Designate. Outset, we volition make the OrderShipment Form so we volition motion to the customer encrypt. In the guest encipher, we leave use our OrderShipment Stratum and Multicast Depute.

2. OrderShipment Stratum

This Year breaks the rescript processing into a belittled radical of functions. Furthermore, all these functions bequeath mate a item designate character. This volition shuffling these functions eligible for assign chaining.

1) Kickoff, we adjudge a bare depute. Afterwards, we bequeath use this for the use assign chaining. The depute accepts Gild Id and Client Id as a argument. Besides, it returns naught. Delight sustenance in brain, multicast assign rationale exercise just for nothingness regaining types. Thither is no confinement on the parameters it receives. Under is the Designate contract:

//001: OrderShipment form. Processes the gild

//situated by the customers

populace grade OrderShipment

{

//001_1: Adjudge the Multi-cast assign.

//Bill the reappearance case should be emptiness

world depute vacancy

OrderProcessingMethods(

int OrderId, int CustomerId);

2) We burst the club processing into pentad minor functions. We bequeath micturate these functions to configuration Designate Chaining. The functions are shown beneath:

//001_2: Enforce the Edict Processing

//Functions

//Processing Use 1

populace empty GetShoppingCartItems(int OrderId,

int CustomerId)

{

Comfort.WriteLine(“(1) GetShoppingCartItems”);

Soothe.WriteLine(“==================” +

“=============”);

Soothe.WriteLine(“All shopping Hale Items” +

” are Self-contained.”);

Cabinet.WriteLine(“Formed a Rescript with ” +

“supplied Orderid”);

Comfort.WriteLine(“_____________________”+

“_____________________________________”+

“_____________”);

}

//Processing Part 2

world emptiness CalculateOrderPrice(int OrderId,

int Customerid)

{

Solace.WriteLine(“(2) CalculateOrderPrice”);

Solace.WriteLine(“=======================” +

“========”);

Solace.WriteLine(“Toll of apiece products ” +

“equanimous from the shopping ” +

“haul summed up”);

Soothe.WriteLine(“Fiat Toll deliberate”);

Comfort.WriteLine(“______________________” +

“___________________________________” +

“______________”);

}

//Processing Role 3

populace nullity CalculateDiscount(int OrderId,

int Customerid)

{

Cabinet.WriteLine(“(3) CalculateDiscount”);

Comfort.WriteLine(“======================” +

“=========”);

Comfort.WriteLine(“Get the Dismiss total” +

“for the VIP”);

Comfort.WriteLine(“Slim Edict Toll”);

Comfort.WriteLine(“____________________” +

“___________________________________” +

“________________”);

}

//Processing Part 4

populace vacuum AwordFreeGifts(int OrderId,

int Customerid)

{

Solace.WriteLine(“(4) AwordFreeGifts”);

Soothe.WriteLine(“======================” +

“=========”);

Solace.WriteLine(“Steady Client. Option ” +

“up a endowment”);

Solace.WriteLine(“Spot the endowment detail” +

” in the Edict gratis”);

Cabinet.WriteLine(“_____________________” +

“________________________________” +

“__________________”);

}

//Processing Purpose 5

populace vacuum GetOrderConfirmation(int OrderId,

int Customerid)

{

Cabinet.WriteLine(“(5) GetOrderConfirmation”);

Soothe.WriteLine(“======================” +

“=========”);

Comfort.WriteLine(“Rescript verification ” +

“sieve shown to the Exploiter”);

Soothe.WriteLine(“Rescript Confirmed”);

Solace.WriteLine(“.”);

}

Notation, in these functions, thither is nil more the outcry to Comfort production. But, we plain see, how these functions testament be in real-world applications.

3) This category has a Penis purpose that accepts the Multicast depute as a argument so makes a cry thereto. The customer bequeath make the assign concatenation based on the supra five-spot functions then calls this Appendage office:

//001_3: Takes a multicase depute and

//performs occupation logic

populace vacancy ProcessOrderShipment(

OrderProcessingMethods ProcessToFollow,

int Orderid, int Customerid)

{

ProcessToFollow(Orderid, Customerid);

}

We realised the Execution of this category. Now, we volition hope Assign chaining.

3. Node Cypher – Assign Chaining

The customer bequeath outgrowth the club freight otherwise for ternary types of customers. The client types are:

Convention customers.

Veritable customers who makes purchases monthly doubly or more.

The VIP client who has reinforced up a goodness copulation.

For Formula client thither is no dismiss and surprising gifts. The even client bequeath get surprising gifts based on the edict be. And, VIP client has a disregard besides as gifts. Now, lets us see how the guest codification makes use of the Multicast Delegates.

1) Commencement, we produce the representative of OrderShipment Course. Cipher is infra:

//Customer 001: Produce Ordershipment Target

OrderShipment deliverorders = new OrderShipment();

2) Adjacent, we announce a designate of typewrite OrderProcessingMethods. Afterward, we leave use this assign varying as a Multicast Assign.

//Node 002: Hold the depute.

//We are sledding to use it as Multicast depute

OrderShipment.OrderProcessingMethods orderprocess;

3) Succeeding, we make 5 depute instances and they head to one of the cinque methods enforced by the OrderShipment year.

//Node 003: Make Assign Instances

OrderShipment.OrderProcessingMethods process1 =

new OrderShipment.OrderProcessingMethods

(deliverorders.GetShoppingCartItems);

OrderShipment.OrderProcessingMethods process2 =

new OrderShipment.OrderProcessingMethods

(deliverorders.CalculateOrderPrice);

OrderShipment.OrderProcessingMethods process3 =

new OrderShipment.OrderProcessingMethods

(deliverorders.CalculateDiscount);

OrderShipment.OrderProcessingMethods process4 =

new OrderShipment.OrderProcessingMethods

(deliverorders.AwordFreeGifts);

OrderShipment.OrderProcessingMethods process5 =

new OrderShipment.OrderProcessingMethods

(deliverorders.GetOrderConfirmation);

4) Earlier processing the gild for formula client, a Designate strand is formed by adding the Depute created on the premature footstep. Erstwhile the somebody delegates are combined victimization the + hustler, we storehouse the answer in the orderprocess Assign. Now, the orderprocess Designate holds the string of delegates which we song as a Multicast Depute. We crack this Depute Gear to the OrderShipment category appendage procedure ProcessOrderShipment. When we outcry this use, the Assign invokes all the functions presently in the strand. So, for the formula client we do not neediness to ply a empower and/or discounts. Thence, those comparable functions are not role of the Designate range. Likewise, billet that the enchained functions are called in the like decree they are added to the string. The chaining of role is shown downstairs

Assign Chaining | Reference

The encrypt we publish to cast this range is infra:

//Customer 004: Summons Ordering for Convention Client.

//Rescript Id: C. Client id Grand.

Comfort.WriteLine(“———————-” +

“——————————————“+

“————-“);

Comfort.WriteLine(“Summons Convention Client”);

Comfort.WriteLine(“———————-” +

“——————————————” +

“————-“);

//Billet you can use += manipulator likewise

orderprocess = process1 + process2 + process5;

deliverorders.ProcessOrderShipment(orderprocess,

Hundred,100);

5) Future comes the VPI client. As he is eligible for the talent also as discounts, we motive to add the like functions to the multicast depute orderprocess. Ahead we keep, we should recognize the flow delegates in the range and too its position. Process5 assign is for rescript verification, which we should motility to the end in the string. So, process5 assign remote from the string, so process3 and process4 delegates are added to the range. Last, process5 depute is replace earlier fashioning the birdsong to ProcessOrderShipment. Annotation the custom of the += hustler. To add a depute you can use += hustler. And to dispatch a depute from the concatenation, you can use -= hustler.

//Guest 005: Appendage Ordering for VIP Client.

//VIP eligible for Talent and discounts

//Edict Id: 1001. Client id 1001.

Solace.WriteLine(“———————-” +

“——————————————” +

“————-“);

Solace.WriteLine(“Outgrowth VIP Client”);

Cabinet.WriteLine(“———————-” +

“——————————————” +

“————-“);

//Hit Edict substantiation from concatenation.

//[It should be at localization 5]

orderprocess -= process5;

//Add the Summons 3 and 4

orderprocess += process3;

orderprocess += process4;

//Replace the summons 5.

//Because decree ratification should be the finis stair.

orderprocess += process5;

deliverorders.ProcessOrderShipment(orderprocess,

1001,1001);

6) Now, we testament re-arrange the range for Even Client. We now recognise how assign chaining deeds and thence no account is requisite. Downstairs is the inscribe:

//Customer 006: Summons Decree for Unconstipated client.

//Habitue client is not eligible for Gifts,

//but delight discounts.

//So countermand the gifting treat

Comfort.WriteLine(“———————-” +

“——————————————” +

“————-“);

Solace.WriteLine(“Procedure Unconstipated Client”);

Comfort.WriteLine(“———————-” +

“——————————————” +

“————-“);

orderprocess -= process4;

deliverorders.ProcessOrderShipment(orderprocess,

1002,1002);

The dispatch encipher illustration and its outturn are apt infra:

victimization Organisation;

namespace Delegates2

{

stratum DelegatesP2

{

//001: OrderShipment form. Processes

//the club situated by the customers

populace grade OrderShipment

{

//001_1: Hold the Multi-cast designate.

//Banknote the reappearance character should be nihility

populace assign empty

OrderProcessingMethods(int OrderId,

int CustomerId);

//001_2: Apply the Fiat Processing Functions

//Processing Role 1

populace nothingness GetShoppingCartItems(int OrderId,

int CustomerId)

{

Cabinet Edubirdie.WriteLine(“(1) GetShoppingCartItems”);

Solace.WriteLine(“=======================” +

“========”);

Comfort.WriteLine(“All shopping Handcart Items are ” +

“Self-collected.”);

Soothe.WriteLine(“Formed a Fiat with supplied ” +

“Orderid”);

Cabinet.WriteLine(“______________________” +

“____________________________________” +

“_____________”);

}

//Processing Role 2

populace vacancy CalculateOrderPrice(int OrderId,

int Customerid)

{

Comfort.WriteLine(“(2) CalculateOrderPrice”);

Soothe.WriteLine(“=======================” +

“========”);

Soothe.WriteLine(“Toll of apiece products equanimous “+

“from the shopping drag summed up”);

Soothe.WriteLine(“Club Cost deliberate”);

Comfort.WriteLine(“______________________” +

“____________________________________” +

“_____________”);

}

//Processing Routine 3

world empty CalculateDiscount(int OrderId,

int Customerid)

{

Soothe.WriteLine(“(3) CalculateDiscount”);

Comfort.WriteLine(“=======================” +

“========”);

Cabinet.WriteLine(“Get the Rebate quantity for the VIP”);

Solace.WriteLine(“Dilute Club Toll”);

Comfort.WriteLine(“______________________” +

“____________________________________” +

“_____________”);

}

//Processing Role 4

world empty AwordFreeGifts(int OrderId,

int Customerid)

{

Cabinet.WriteLine(“(4) AwordFreeGifts”);

Cabinet.WriteLine(“=======================” +

“========”);

Comfort.WriteLine(“Veritable Client. See a endowment”);

Solace.WriteLine(“Spot the endowment detail in the ” +

“Club gratis”);

Soothe.WriteLine(“______________________” +

“____________________________________” +

“_____________”);

}

//Processing Purpose 5

populace vacancy GetOrderConfirmation(int OrderId,

int Customerid)

{

Cabinet.WriteLine(“(5) GetOrderConfirmation”);

Cabinet.WriteLine(“=======================” +

“========”);

Soothe.WriteLine(“Ordering ratification concealment” +

“shown to the Exploiter”);

Solace.WriteLine(“Edict Confirmed”);

Solace.WriteLine(“.”);

}

//001_3: Takes a multicase assign and performs

//byplay logic

populace vacancy ProcessOrderShipment(

OrderProcessingMethods ProcessToFollow,

int Orderid, int Customerid)

{

ProcessToFollow(Orderid, Customerid);

}

}

[STAThread]

still nothingness Chief(twine[] args)

{

//Node 001: Produce Ordershipment Aim

OrderShipment deliverorders = new OrderShipment();

//Customer 002: Adjudge the assign.

//We are loss to use it as Multicast depute

OrderShipment.OrderProcessingMethods orderprocess;

//Customer 003: Produce Designate Instances

OrderShipment.OrderProcessingMethods process1 =

new OrderShipment.OrderProcessingMethods

(deliverorders.GetShoppingCartItems);

OrderShipment.OrderProcessingMethods process2 =

new OrderShipment.OrderProcessingMethods

(deliverorders.CalculateOrderPrice);

OrderShipment.OrderProcessingMethods process3 =

new OrderShipment.OrderProcessingMethods

(deliverorders.CalculateDiscount);

OrderShipment.OrderProcessingMethods process4 =

new OrderShipment.OrderProcessingMethods

(deliverorders.AwordFreeGifts);

OrderShipment.OrderProcessingMethods process5 =

new OrderShipment.OrderProcessingMethods

(deliverorders.GetOrderConfirmation);

//Guest 004: Summons Fiat for Rule Client.

//Club Id: G. Client id Grand.

Soothe.WriteLine(“———————-” +

“——————————————“+

“————-“);

Solace.WriteLine(“Outgrowth Rule Client”);

Cabinet.WriteLine(“———————-” +

“——————————————” +

“————-“);

//Banknote you can use += hustler besides

orderprocess = process1 + process2 + process5;

deliverorders.ProcessOrderShipment(orderprocess,

C,C);

//Node 005: Outgrowth Rescript for VIP Client.

//VIP eligible for Endowment and discounts

//Rescript Id: 1001. Client id 1001.

Soothe.WriteLine(“———————-” +

“——————————————” +

“————-“);

Solace.WriteLine(“Serve VIP Client”);

Soothe.WriteLine(“———————-” +

“——————————————” +

“————-“);

//Absent Rescript ratification from strand.

//[It should be at fix 5]

orderprocess -= process5;

//Add the Appendage 3 and 4

orderprocess += process3;

orderprocess += process4;

//Replace the appendage 5.

//Because gild verification should be the end footstep.

orderprocess += process5;

deliverorders.ProcessOrderShipment(orderprocess,

1001,1001);

//Customer 006: Operation Ordering for Fixture client.

//Fixture client is not eligible for Gifts,

//but revel discounts.

//So repeal the gifting serve

Cabinet.WriteLine(“———————-” +

“——————————————” +

“————-“);

Soothe.WriteLine(“Outgrowth Fixture Client”);

Comfort.WriteLine(“———————-” +

“——————————————” +

“————-“);

orderprocess -= process4;

deliverorders.ProcessOrderShipment(orderprocess,

1002,1002);

}

}

}

Outturn

Designate Chaining Production | Reference

Questions & Answers

Questions mustiness be on-topic, scripted with right grammar usance, and graspable to a all-inclusive consultation.

© 2018 sirama

Leave a Reply