C# delegate dictionary add -
i want create method this:
private static void addorappend<k>(this dictionary<k, multicastdelegate> firstlist, k key, multicastdelegate newfunc) { if (!firstlist.containskey(key)) { firstlist.add(key, newfunc); } else { firstlist[key] += newfunc; // line fails } }
but fails because says can't add multicast delegates. there i'm missing? thought delegate keyword shorthand class inherits multicastdelegate.
firstlist[key] = (multicastdelegate)delegate.combine(firstlist[key],newfunc);
with test:
var data = new dictionary<int, multicastdelegate>(); action action1 = () => console.writeline("abc"); action action2 = () => console.writeline("def"); data.addorappend(1, action1); data.addorappend(1, action2); data[1].dynamicinvoke();
(which works)
but tbh, use delegate
in place of multicastdelegate
; largely hangover never worked. or better; specific type of delegate (perhaps action
).
Comments
Post a Comment