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 9171157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:07:29+00:00 2026-06-17T16:07:29+00:00

Lets imagine simple delegate calls: void Main() { Func<int, int, string> tfunc = null;

  • 0

Lets imagine simple delegate calls:

void Main()
{
    Func<int, int, string> tfunc = null;
    tfunc += Add; // bind first method
    tfunc += Sub; // bind second method 

    Console.WriteLine(tfunc(2, 2));
}

private string Add(int a, int b)
{
    return "Add: " + (a + b).ToString();
}

private string Sub(int a, int b)
{
    return "Sub: " + (a - b).ToString();
}

The result of this program is:

Sub: 0

So, why Add method was not called? I’m expecting to call Method Add, and then method Sub.

  • 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-17T16:07:30+00:00Added an answer on June 17, 2026 at 4:07 pm

    Add was correctly chained and called, take a look at the result of

    void Main()
    {
        Func<int, int, string> tfunc = null;
        tfunc += Add; // bind first method
        tfunc += Sub; // bind second method 
    
        Console.WriteLine(tfunc(2, 2));
    }
    
    private string Add(int a, int b)
    {
        Console.WriteLine("Inside Add");
        return "Add: " + (a + b).ToString();
    }
    
    private string Sub(int a, int b)
    {
        Console.WriteLine("Inside Sub");
        return "Sub: " + (a - b).ToString();
    }
    

    It is:

    Inside Add
    Inside Sub
    Sub: 0
    

    What is not chained, because there is no way to access it, is the result of the Add method. Delegates that return a value, in case of chaining, return the value of the last method invoked, that is the last method that was added to the delegate.

    This is specified in part 15.4 of the C# 4.0 language specification

    Invocation of a delegate instance whose invocation list contains
    multiple entries proceeds by invoking each of the methods in the
    invocation list, synchronously, in order. …
    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.

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

Sidebar

Related Questions

Imagine a simple Action with a post public ActionResult Unsubscribe(string contentId) { // get
yeah :( simple question * blush * Lets imagine i have the following enumeration:-
Imagine a simple case like this: class Book has_many :chapters end Let's say in
Let's imagine I want to make a templated function that returns the first element
Let's imagine really simple game... We have a labirinth and two players trying to
Imagine a simple 2D grid; the objects on the grid can occupy more then
Let's imagine this simple, I have a div (myDiv) inside a form which includes
So let's imagine a simple IS-A relationship like this example: create table EntityAbstract( IDEntityAbstract
Let's imagine the following situation: You have a simple php page with an html
Lets imagine we have 2 classes (A and B), with 2 samples in each

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.