Answer the question
In order to leave comments, you need to log in
C# What happens when we pass a delegate to a method and add 2 methods to it?
I'm doing an experiment right now:
class Program
{
delegate void Operation();
public static void Main()
{
Operation op = DoSomething;
Update(op);
op(); \\DoSomething();
}
public static void Update(Operation op)
{
op += DoSomething2;
op += DoSomething3;
op(); \\DoSomething();, DoSomething2();,
\\DoSomething3();
}
public static void DoSomething()
{
}
public static void DoSomething2()
{
}
public static void DoSomething3()
{
}
}
Answer the question
In order to leave comments, you need to log in
I wrote to you yesterday in your question about the lines I wrote, and so here it goes the same way. Before calling the function, your delegate reference is duplicated on the stack. Since delegates are immutable, each time you add a method to a delegate, a new delegate will be created and a reference will be written to a copy invisible to you, without touching the variable. This is protection against unexpected behavior. Same behavior as with strings. If you want magic, pass the parameter via ref.
Yes, and frame the code in tags.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question