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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:24:45+00:00 2026-05-25T21:24:45+00:00

I have a simple program that doesn’t behave the way I expected it to

  • 0

I have a simple program that doesn’t behave the way I expected it to behave. I was under the impression that both Method Signatures would run in the order of the Invocation List of the Delegate during the CallEvent() method and make the Test Property equate to 40. Hence:

Test += (20+15);
Test += (20-15);
Test == 40;

As it would turn out, this assignment is equal to 5, which is the value of the subtraction. If it did the addition first then replaced it with subtraction, doesn’t that defeat the purpose of the Test += assignment? Maybe it is bypassing the addition all together (however unlikely). I suspect something more inherent is going on that I just can’t see with my current programming knowledge.

Please and Thank You!
LiquidWotter

//MY NAMESPACE
namespace MyNamespace
{
    //MAIN
    private static void Main(string[] args)
    {
        //CREATE MY OBJECT
        MyClass MyObject = new MyClass();

        //ADD CALLS TO EVENT
        MyObject.MyEvent += Add;
        MyObject.MyEvent += Sub;

        //CALL EVENT AND WRITE
        MyObject.CallEvent(20, 15);
        Console.WriteLine(MyObject.Test.ToString());

        //PAUSE
        Console.ReadLine();
    }

    //ADDITION
    private static int Add(int x, int y)
    { return x + y; }

    //SUBTRACTION
    private static int Sub(int x, int y)
    { return x - y; }

    //MY CLASS
    public class MyClass
    {
        //MEMBERS
        public delegate int MyDelegate(int x, int y);
        public event MyDelegate MyEvent;
        public int Test { get; set; }

        //CONSTRUCTOR
        public MyClass()
        {
            this.Test = 0;
        }

        //CALL Event
        public void CallEvent(int x, int y)
        {
            Test += MyEvent(x, y);
        }
    }
}
  • 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-25T21:24:46+00:00Added an answer on May 25, 2026 at 9:24 pm

    Well you are correct. There can only be one return value[1], while both delegates where called only one, in fact the last, returned its value. Which makes sense, because how should the framework know what to do in cases where you directly place your event in a method as an argument:

      this.Foo( MyEvent(x, y) );
    

    call Foo once or multiple times? somehow combine the values? You see it is not clear.

    I have to add that the order of delegates is also not defined[2], well currently it is defined(The order of registration) but you should never rely on it.

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

Sidebar

Related Questions

Say I have simple program that emulates a board game with a number of
I have a simple program that creates a thread, loops twenty times and then
I have a simple python program that I'd like to daemonize. Since the point
I have a simple forms program that I have been fighting with for a
I have a simple Delphi program that I'm working on, in which I am
I have a simple Tray icon program that opens a site using System.Diagnostics.Process.Start(URL) And
I have two simple while loops in my program that I feel ought to
I have a relatively simple select statement in a VB6 program that I have
I have to write a program that sniffs network packets (part1-the simple part). And
We have a fairly simple program that's used for creating backups. I'm attempting to

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.