Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4597464
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:15:39+00:00 2026-05-21T23:15:39+00:00

Since we register the callback function PrintOne twice, the following code will print the

  • 0

Since we register the callback function PrintOne twice, the following code will print the message “PrintOne” twice. Here are questions,

Question 1> Why by default the operator+=(i.e. Combine) doesn’t check duplicate method handler?

Question 2> How to avoid this duplicated call in method RegisterCall? I try to find some method in MulticastDelegate/Delegate that can tell me there is already one in the calling list. But I didn’t find it.
http://msdn.microsoft.com/en-us/library/system.multicastdelegate.aspx

Thank you

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace app3
{
    class Car
    {
        public delegate void PrintMethod(string msg);

        public string Name { get; set; }

        private PrintMethod printMethods;

        public Car() { }
        public Car(string name) { Name = name; }

        public void PrintCar()
        {
            if (printMethods != null)
            {
                printMethods(this.ToString());
            }
            else
            {
                Console.WriteLine("No Method will be called");
            }
        }
        public override string ToString()
        {
            return string.Format("Car Name is {0}: ", Name);
        }

        public static void PrintOne(string msg)
        {
            Console.WriteLine("PrintOne");
        }

        public static void PrintTwo(string msg)
        {
            Console.WriteLine("PrintTwo");
        }

        public void RegisterCall(PrintMethod methodToCall)
        {
            printMethods += methodToCall;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Car mycar = new Car { Name = "BMW" };
            mycar.RegisterCall(new Car.PrintMethod(Car.PrintOne)); // **will print for the first time**
            mycar.RegisterCall(new Car.PrintMethod(Car.PrintOne)); // **will print for the second time**
            mycar.PrintCar();

            Console.ReadLine();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:15:40+00:00Added an answer on May 21, 2026 at 11:15 pm
    public void RegisterCall(PrintMethod methodToCall)
    {
        printMethods -= methodToCall;
        printMethods += methodToCall;
    }
    

    This will make sure it is removed if present in the multicast delegate, then added, to ensure 1 instance.

    Adding a handler doesn’t abort if a delegate to the same handler is already present in the multicast delegate because most of the time duplicates don’t happen. There are also valid situations in which calling the same method twice is what is wanted (such as custom aggregation on an object or collection).

    If they had decided that duplication would be avoided, they would have to throw an exception when adding the handler. That’s expensive in a number of ways, both when they occur at runtime and in all the ugly try-catch blocks we’d have to write.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since I didn't register the property how would I add a property changed callback?
Given the following code: container.Register( AllTypes.FromAssemblyNamed(MyNameSpace) .Where(component => component.Name.EndsWith(Repository)) .WithService.FirstInterface() .Configure(component => component.LifeStyle.Singleton) );
I am using the FB registration plugin to register users (Since I want a
I've created some new fields for users who register in admin/config/people/profile since we are
I'm trying to set a register to 12000. Since MOV can't put a value
I'm attempting to register an anonymous function when a user clicks a cell in
I'm facing some questions since they're not clearly covered in the IA-32 assembly ebook
I need to run some code to register a type for a factory pattern.
NOTE: I have updated this since originally asking the question to reflect some of
I am struggling since 2 -3 hours to register the Interop.AZROLESLib.dll to gac using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.