10

im kinda new to c#, so I came up with this problem. Question: why is func2 called? oh, and one more thing. say I add a function to a delegate. In this function I call another delegate, however I want to make sure that every other function added to the first delegate is called before this function calls this delegate, is there any clean solution ( not really interested in getInvocationList). Thanks guys, you're the best.

class Program
{
    delegate void voidEvent();
    voidEvent test;

    private void func1()
    {
        Console.Write("func1");
        test -= func2;
    }

    private void func2()
    {
        Console.WriteLine("func2");
    }

    static void Main(string[] args)
    {
        Program p = new Program();
        p.test += p.func1;
        p.test += p.func2;
        p.test();
    }
}

2 Answers 2

21

Every time you change a delegate (+= or -=), you're effectively creating an entire copy of the invocation list (the methods that will get called).

Because of that, when you call p.test();, you're going to invoke every delegate in the invocation list at that point in time. Changing this inside of one of those handlers will change it for the next call, but won't change the currently executing call.

3
  • 3
    +1. Yes. This is why you can write var func = p.test; if (func != null) { func(); } in multithreading scenarios without locking anything. Because func is a copy of p.test and does not care of changes made to p.test. Apr 5, 2012 at 23:30
  • but after I call test-= func2 and call getinvocationlist i can see func2 is no more in the list. So you're saying every delegate has two lists, one that is currently being invoked ( and is not reachable ) and one that will be invoked next time?
    – Yamcha
    Apr 6, 2012 at 8:50
  • @user1316459, it doesn't really have two lists. But when you invoke a delegate from a filed, you effectively say something like: “take the value of the field right now and then invoke that”. That means you're invoking a copy of the value of test, not test itself.
    – svick
    Apr 6, 2012 at 13:18
7

Reed is of course correct. Here's another way to think about it.

class Number
{
    public static Number test;
    private int x;
    public Number(int x) { this.x = x; }
    public Number AddOne() 
    {
        return new Number(x + 1);
    }
    public void DoIt()
    {
        Console.WriteLine(x);
        test = test.AddOne();
        Console.WriteLine(x);
    }
    public static void Main()
    {
        test = new Number(1);
        test.DoIt(); 
    }
}

Should that print 1, 1, or 1, 2? Why?

It should print 1, 1. When you say

test.DoIt();

that does not mean

        Console.WriteLine(test.x);
        test = test.AddOne();
        Console.WriteLine(test.x);

! Rather, that means

Number temporary = test;
Console.WriteLine(temporary.x);
test = test.AddOne();
Console.WriteLine(temporary.x);

Changing the value of test does not change the value of this in DoIt.

You're doing exactly the same thing. Changing the value of test does not change the list of functions that you are invoking; you asked for a particular list of functions to be invoked and that list is going to be invoked. You don't get to change it halfway through, any more than you get to change the meaning of this halfway through a method call.

1
  • 1
    You appear to be missing the field x in your definition of Number.
    – kvb
    Apr 6, 2012 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.