c# - "Field-like events" and "events in general" clarification? -
a field-like-event
way of declaring both delegate variable , event @ same time.
so field event (public event eventhandler myevent;
) may translate : ( taken here)
private eventhandler _myevent; public event eventhandler myevent { add { lock (this) { _myevent += value; } } remove { lock (this) { _myevent -= value; } } }
notice private backup field.
however , corrected jon ( comments section) , general-event doen't have backup field . :
public event eventhandler myevent { add { console.writeline ("add operation"); } remove { console.writeline ("remove operation"); } }
notice - no backup field.
but said winform act :
for instance, in situations there lots of events few subscribed to, have map key describing event delegate handling it. windows forms - means can have huge number of events without wasting lot of memory variables have null values.
question :
- how can winform use map thing expose events without backup field ( of delegate type)
first of "map thing" dictionary<object, eventhandler>
. field of class when created contain no elements , therefore not take memory (just overhead internal structures). events subscribed delegates added dictionary , start use more memory.
consider class 50 events use 5 events, in simple scenario there 50 delegates created @ every instance of class. when using dictionary storage there 5 delegates per instance.
p.s. same principle behind dependency objects/properties in wpf.
Comments
Post a Comment