Oct 16, 2014

Delegates act as value types

Action a1 = () => Console.Write("1 ");
Action a2 = () => Console.Write("2 ");
Action tmp = a1;
a1 += a2;
a2 = tmp;
Console.Write("a1:\t");
a1();   //1 2
Console.Write("\na2:\t");
a2();   //1
Console.ReadLine();

its because delegates are immutable, just like strings.
a1 += a2 is a shugar for a1 = (Action)Delegate.Combine(a1, a2)

No comments:

Post a Comment