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

  • Home
  • SEARCH
  • 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 7930461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:25:57+00:00 2026-06-03T20:25:57+00:00

On button1_click one messagebox after another appear – first showing 30 the second showing

  • 0

On button1_click one messagebox after another appear – first showing 30 the second showing 200:

public partial class Form1 : Form
{

    delegate void myMathFunction(int j, int q);

    void add(int x, int y) {MessageBox.Show((x + y).ToString());}

    void multiply(int x, int y){MessageBox.Show((x*y).ToString());}

    private void button1_Click(object sender, EventArgs e)
    {
        myMathFunction foo = new myMathFunction(add);
        foo+= new myMathFunction(multiply);

        foo.Invoke(10, 20);
    }

    public Form1() { InitializeComponent(); }
}

but the following just goes straight to the 200 and yet I’ve assigned both methods to the delegate – what has happened to add and why does it choose to use multiply?

public partial class Form1 : Form
{

    delegate int myMathFunction(int j, int q);

    int add(int x, int y){return x + y;}

    int multiply(int x, int y) {return x * y;}

    private void button1_Click(object sender, EventArgs e)
    {
        myMathFunction foo = new myMathFunction(add);
        foo += new myMathFunction(multiply);

        MessageBox.Show(foo.Invoke(10, 20).ToString());
    }

    public Form1() { InitializeComponent(); }
}

Can the second code sample be amended so that the delegate runs the add method rather than the multiply method?

  • 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-06-03T20:25:59+00:00Added an answer on June 3, 2026 at 8:25 pm

    When your delegate has multiple functions attached to it, each one is invoked in turn. If the delegate has a non-void return value, the return value of the last function is what is returned.

    The language specification, 15.4 Delegate invocation, says

    If the delegate invocation includes output parameters or a return value, their final value will come from the invocation of the last delegate in the list.

    So, when you call foo.Invoke(10, 20), the following happens:

    • First, add(10, 20) is called which returns 30.
    • Then, multiply(10, 20) is called which returns 200 and that value is returned to the original caller.

    In your follow-up question you ask

    Can the second code sample be amended so that the
    delegate runs the add method rather than the multiply
    method?

    As explained above, the add method and the multiply method are both executed. The return value from the last executed method is what is returned to the caller. So, if you want the value resulting from calling add to be returned, it must be the last method added to the delegate instance.

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

Sidebar

Related Questions

I am facing a problem while disabling the command button after one click. I
In a WPF application, I've got my events centralized in one class like this:
I've got these two methods in 1 class (Class CokeMachine): public void MoneyButtonEvents() {
I have the problem to retreive the string from one form to another. here
I have an a windows form application that use one class (its name is
In my application i want send a email to one account.in one button click
Adding the row one by one in the button click.. (i tried but its
In my application when I click on one of my button in the page
I have one issue with UInavigationcontroller In my firstview i have button.If i click
How to Send email one time 20 emails only codeIgniter? When click send button

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.