---
title: "The problem with Multi-Cast Delegates"
url: "https://osherove.com/blog/2004/3/27/the-problem-with-multi-cast-delegates.md"
canonical: "https://osherove.com/blog/2004/3/27/the-problem-with-multi-cast-delegates.html"
published: "2004-03-27T20:01:00"
updated: "2004-03-27T20:01:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "Here’s a very interesting situation you can get yourself into when working with delegates: you have a delegate that returns a result. You use this delegate as part of your calculation. What happens when you have…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# The problem with Multi-Cast Delegates

By Roy Osherove March 27, 2004 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published March 27, 2004; technical details may be outdated.

Here’s a very interesting situation you can get yourself into when working with delegates: you have a delegate that returns a result. You use this delegate as part of your calculation. What happens when you have multiple targets on the delegate, each returning a different result? _Here’s code that demonstrates this:_

**Class1** has a calculation delegate that receiv different result? Here’s  code that demonstrates this:

Class1 has a calculation es 2 numbers and returns a result. It uses this delegate as part of some simple algorithm. The **MyClient** class attaches 2 different callbacks to the same delegate of **Class1**, each returning a different result.

What will be the result when **MyClient** actually calls the **DoDelegateWork**() function on **Class1**?

public delegate int CalculationDelegate(int num1,int num2);

public class Class1

{

      public CalculationDelegate DoCalculation;

      public int DoDelegateWork()

      {

            if(DoCalculation!=null)

            {

                  int result\= DoCalculation(3,3);

                  return result;

            }

            return 0;

      }

}

public class MyClient

{

public void AttachToDelegate()

{

Class1 c = new Class1();

c.DoCalculation+=new CalculationDelegate(c\_DoCalculation);

c.DoCalculation+=new CalculationDelegate(c\_DoCalculation2);

}

private int c\_DoCalculation(int num1, int num2)

{

      return num1\*num2;

}

private int c\_DoCalculation2(int num1, int num2)

{

      return num1+num2;

}

}

The answer is that whatever target in the invocation list was invoked last will be used to return the actual result. All the other results are discarded. Pretty crazy. It’s pretty weird that we are actually able to do something like this in the framework, and that there is no simple way to avoid this predicament. (I stress, no **simple** way. We can just through a few hoops to make sure this does not happen, but there is no built in support construct that will deny this from happening. This effect is caused by the fact that all delegates are actually **Multicast** Delegates.

(**PS**: this same code works just as well whether the delegate is declared as an event or not. Events were never meant to be used this way, but hey, it's good to know you can of you really need to in some twisted way!)

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- Mar 28, 2004

    ### [Troubled comments in XML documents](https://osherove.com/blog/2004/3/28/troubled-comments-in-xml-documents.html)

- Mar 27, 2004

    ### [Israel .Net Architects user group meeting today :Service Oriented Architecture](https://osherove.com/blog/2004/3/27/israel-net-architects-user-group-meeting-today-service-orien.html)

- Mar 27, 2004

    ### [Delegates & Events - A short Q&A](https://osherove.com/blog/2004/3/27/delegates-amp-events-a-short-qampa.html)

- Mar 24, 2004

    ### [The Regulator moves to SourceForge, slowly but surely](https://osherove.com/blog/2004/3/24/the-regulator-moves-to-sourceforge-slowly-but-surely.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
