I'm new to delegates and as far as I understood you can add two functions or even more to a delegate (using +=
). But when you activate a delegate it will always call the last function added. What if I wanted to call a function that I added before? let's say I have:
public static int add(int o1, int o2)
{
return o1 + o2;
}
public static int multiply(int o1, int o2)
{
return o1 * o2;
}
public static int minus(int o1, int o2)
{
return o1 - o2;
}
So I use a delegate (actually a Func
) to add all those functions.
Func<int, int, int> f;
f = add;
f += multiply;
f += minus;
Now lets say I want to call multiply
or add
, I cant do this, if I use:
Console.WriteLine(f(1,2));
It will only call minus
.
edit
By the way, I'm aware of the possibility to remove function using -=
, but if there is a large number of functions, its not convenient. the kind of solutions I'm looking for is indexes (like in arrays) however
f[2](5,3)
Doesn't seem to work.