c# - Event parameter; "sender as Object", or "sender as T"? -
when write public events business objects, i've adapted habit of passing instance "sender object", in addition additional specific parameters. asked myself why not specifying class?
so more experience; do ever pass distinct class sender in event? , if so, decision criteria when ok/not ok?
don't extreme. eventhandler(object sender, eventargs e)
has object sender can use in many circumstances. doesn't mean strongly-typed sender evil. strongly-typed sender useful when delegate not going used(like eventhandler
) e.g.
public delegate void savehandler(controller sender, eventargs e);
now other developers(or using library) can recogonize sender have to controller
, , glad not code this:
public void mysavehandler(object sender, eventargs arg) { var controller = sender controller; if (controller != null) { //do } else { //throw exception @ runtime? //it can avoided if sender strongly-typed } }
and can make generic:
public delegate void savehandler<t>(t sender, eventargs args) t: icontroller;
it's pure legal , practice in c#. should make clear want do, , choose better way. either of them evil/bad.
Comments
Post a Comment