C # programming guide
How to: trigger base class events in a derived class (C # programming guide)The following simple example demonstrates a standard method for declaring events that can be raised from a derived class in the base class. This mode is widely used in Windows form classes in. NET Framework base class libraries. When creating a base class that can be used as another class, you must consider the fact that an event is a special type delegate and can only be called from the class that declares them. The derived class cannot directly call the Events declared in the base class. Although you may want an event to be triggered only by the base class, in most cases, you should allow the derived class to call the base class event. To this end, you can create a protected call method in the base class that contains the event. By calling or rewriting this call method, the derived class can indirectly call this event.
ExampleC # copy the code namespace baseclassevents {using system; using system. collections. generic; // special eventargs class to hold info about shapes. publicclass shapeeventargs: eventargs {private double newarea; Public shapeeventargs (double D) {newarea = D;} public double newarea {get {return newarea ;}}} // base class event publisher public abstract class shape {protected double area; public double are A {get {return area;} set {area = value ;}// the event. note that by using the generic eventhandler <t> event type // we do not need to declare a separate delegate type. public event eventhandler <shapeeventargs> shapechanged; public abstract void draw (); // The Event-invoking method that derived classes can override. protected virtual void onshapechanged (shapeeventargs e) {// make a temporar Y copy of the event to avoid possibility of // a race condition if the last subscriber unsubscribes // immediately after the null check and before the event is raised. eventhandler <shapeeventargs> handler = shapechanged; If (handler! = NULL) {handler (this, e) ;}} publicclass circle: Shape {private double radius; public circle (double D) {radius = D; area = 3.14 * radius;} publicvoid Update (double D) {radius = D; Area = 3.14 * radius; onshapechanged (New shapeeventargs (area ));} protected override void onshapechanged (shapeeventargs e) {// do any circle-specific processing here. // call the base class event invocation method. base. onshapechanged (E);} public override void draw () {console. writeline ("drawing a circle") ;}} publicclass rectangle: Shape {private double length; private double width; Public rectangle (double length, double width) {This. length = length; this. width = width; Area = length * width;} publicvoid Update (double length, double width) {This. length = length; this. width = width; Area = length * width; onshapechanged (New shapeeventargs (area);} protected override void onshapechanged (shapeeventargs e) {// do any rectangle-specific processing here. // call the base class event invocation method. base. onshapechanged (E);} public override void draw () {console. writeline ("drawing a rectangle") ;}// represents the surface on which the shapes are drawn // subscribes to shape events so that it knows // when to redraw a shape. publicclass shapecontainer {list <shape> _ list; Public shapecontainer () {_ list = new list <shape> ();} publicvoid addshape (shape s) {_ list. add (s); // subscribe to the base class event. s. shapechanged + = handleshapechanged ;}//... other methods to draw, resize, etc. privatevoid handleshapechanged (Object sender, shapeeventargs e) {shape S = (SHAPE) sender; // Diagnostic message for demonstration purposes. console. writeline ("received event. shape area is now {0} ", E. newarea); // redraw the shape here. s. draw () ;}} class test {staticvoid main (string [] ARGs) {// create the event publishers and subscriber circle C1 = new circle (54 ); rectangle R1 = new rectangle (12, 9); shapecontainer SC = new shapecontainer (); // Add the shapes to the container. SC. addshape (C1); SC. addshape (R1); // cause some events to be raised. c1.update (57); r1.update (7, 7); // keep the console window open. console. writeline (); console. writeline ("press enter to exit"); console. readline ();}}}
OutputStored ed event. Shape area is now 178.98 drawing a circlereceived event. Shape area is now 49 drawing a rectangle (Source: msdn)