Multicast Delegate

Multicast Delegate

Multicast Delegate

Multicast Delegate is a delegate which holds references of more than a function pointer having same number of parameters and return types.

We can attach multiple references of method to a single delegate using += sign and those methods will be executed in same sequence as defined.

For example, let’s consider below example


We can attach multiple method to same delegate as below

?

?

Here you can see it called all methods in same sequence as those were referred to delegate object.

?

Many developers know this concept already but got confused where to use these delegates into code/project. It is very important, once you learn a concept, you must also know how to use this and where to use this. Think, of a solider who trained to aim to target, but until and unless he/she doesn’t know to whom he/she needs to shoot someone OR identify the situation when to use his/her aiming skills, it completely useless.

In same way, we also should have clear visibility how to use delegate in real project to make an impact.

So, lets explain where to use in detail. Most of developers are already aware of e-commerce website and you must have seen that delivery charges are free if payment exceed to some amount, but we don’t want to hard code that on front end application rather than we want to implement this inside order class. Also, we have multiple delivery partner to deliver order based upon Address Type.


?In above example, you can see, we haven’t written any logic to calculate delivery fee and choose delivery partner, rather than we declare delegates to process that data.

To achieve this and take out business logic outside Order class, Lets add new class “OrderProcessing” which implement 2 methods.

?

?

?

Now, let’s create few orders to test this

?

?

And we can write some logic to notify delivery partner and add more logic around this.


Also, delegate is used to define which function/method will be add if we add dynamic button to a page OR form.

?

?

?

?

?

?

要查看或添加评论,请登录

Satya Prakash Chhikara的更多文章

  • Checksum

    Checksum

    Checksum Checksum is the outcome of running an algorithm, called a cryptographic hash function on a piece of data…

  • Proxy Server

    Proxy Server

    Proxy Server Definition A proxy server is a router that provides a gateway between users and the internet. It prevents…

  • Lambda Expressions in C#

    Lambda Expressions in C#

    Lambda Expressions in C# Lambda Expressions are shorthand writing for anonymous methods. Let’s look at anonymous method.

  • Anonymous Method with C#

    Anonymous Method with C#

    Anonymous Method As name suggests, anonymous method is a function without having a name. You can say code block with a…

  • Generic Delegates

    Generic Delegates

    Generic Delegate Before discussing generic delegates, it takes an example of delegates. Delegate is a pointer to an…

  • Events, Delegates, Events Args and Event Handler in C#

    Events, Delegates, Events Args and Event Handler in C#

    Events, Delegates, Events Args and Event Handler in C# Events, delegates, events args and event handler are quite…

  • Finalize and Dispose in C#

    Finalize and Dispose in C#

    Finalize and Dispose in C# Finalize method 1. This method is used to release resources before the current object is…

  • Stack and Heap Memory in .NET

    Stack and Heap Memory in .NET

    Stack and Heap Memory in .NET Let’s try to understand what exactly happens when we declare a variable into .

  • Choosing between SQL and NoSQL

    Choosing between SQL and NoSQL

    Choosing between SQL and NoSQL When it comes to choosing database between SQL and NoSQL, it’s become tough and…

  • Dependency Inversion Principle (DIP)

    Dependency Inversion Principle (DIP)

    Dependency Inversion Principle (DIP) DIP states that high level modules/classes should not depend on low level…

社区洞察

其他会员也浏览了