C# When explicit delegate declaration is required when working with events -
this question has answer here:
all know delegate function pointer. when work event declare delegate know in kind of situation have declare delegate when working events public delegate void onbuttonclickdelegate();
here refering code sample show can use in built delegate eventhandler
instead of declare delegate explicitly.
public class argsspecial : eventargs { public argsspecial (string val) { operation=val; } public string operation {get; set;} } public class animal { // empty delegate. in way sure value != null // because no 1 outside of class can change it. public event eventhandler<argsspecial> run = delegate{} public void raiseevent() { run(this, new argsspecial("run faster")); } } animale animal= new animal(); animal.run += (sender, e) => console.writeline("i'm running. value {0}", e.operation); animal.raiseevent();
so when eventhandler
in built delegate solve purpose no need declare delegate explicitly when working events. tell me have declare delegate explicitly , eventhandler
may not solve our purpose. thanks
eventhandler
has limited signature, 2 parameters first type object
, , second has class type inheriting system.eventargs
. considering system.action<t, ...>
, system.func<t, ..., treturn>
, rare need declare another. in fact of original bcl delegate types methodinvoker
, converter<t, u>
, predicate<t>
, , eventhandler<t>
wouldn't have been defined if action , func families had existed @ time.
where still need declare own delegate when need by-reference parameters. ref
, out
keyword cannot passed through generic type parameter, if want delegate matching tryparse
family, you'd unable func<string, out t, bool>
, need custom declaration
/* return true if parsing successful */ delegate bool tryparser<t>(string from, out t into);
it's uncommon use by-reference parameters events, you'll never have declare delegate use event type.
Comments
Post a Comment