An event can being raised only from the declaration space in which it is declared. Therefore, a class cannot raise events from any other class, even one from which it is derived.
An event can only be used in the declaration space (class) it declares. Therefore, it cannot be thrown from any other class, even if the class is the inheriting class of the class in which the event resides.
So how can we trigger the events in the base class and give an example to everyone to see.
namespacebaseclassevents{usingSystem; usingSystem.Collections.Generic; //special EventArgs class to hold info about Shapes. Public classShapeeventargs:eventargs {Private DoubleNewarea; PublicShapeeventargs (Doubled) {Newarea=D; } Public DoubleNewarea {Get{returnNewarea;} } } //Base class Event Publisher Public Abstract classShape {protected DoubleArea ; Public DoubleArea {Get{returnArea ;} Set{area =value;} } //The event. Note that by using the generic eventhandler<t> event type//We don't need to declare a separate delegate type. Public EventEventhandler<shapeeventargs>shapechanged; Public Abstract voidDraw (); //The event-invoking method that derived classes can override. protected Virtual voidonshapechanged (Shapeeventargs e) {//Make a temporary 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); } } } Public classCircle:shape {Private Doubleradius; PublicCircle (Doubled) {radius=D; Area=3.14* RADIUS *radius; } Public voidUpdate (Doubled) {radius=D; Area=3.14* RADIUS *radius; Onshapechanged (NewShapeeventargs (area)); } protected Override voidonshapechanged (Shapeeventargs e) {//Do any circle-specific processing here. //Call the base class event invocation method. Base. Onshapechanged (e); } Public Override voidDraw () {Console.WriteLine ("Drawing a circle"); } } Public classRectangle:shape {Private Doublelength; Private Doublewidth; PublicRectangle (DoubleLengthDoublewidth) { This. length =length; This. width =width; Area= length *width; } Public voidUpdate (DoubleLengthDoublewidth) { This. length =length; This. width =width; Area= length *width; Onshapechanged (NewShapeeventargs (area)); } protected Override voidonshapechanged (Shapeeventargs e) {//Do any rectangle-specific processing here. //Call the base class event invocation method. Base. Onshapechanged (e); } Public Override voidDraw () {Console.WriteLine ("Drawing a rectangle"); } } //represents the surface on which the shapes is drawn//subscribes to shape events so that it knows//When to redraw a shape. Public classShapeContainer {List<Shape>_list; PublicShapeContainer () {_list=NewList<shape>(); } Public voidAddShape (Shape s) {_list. ADD (s); //Subscribe to the base class event.S.shapechanged + =handleshapechanged; } // ... Other methods to draw, resize, etc. Private voidHandleshapechanged (Objectsender, Shapeeventargs e) {Shape s=(Shape) sender; //Diagnostic message for demonstration purposes.Console.WriteLine ("Received event. Shape area was now {0}", E.newarea); //Redraw the shape here.S.draw (); } } classTest {Static voidMain (string[] args) { //Create The event Publishers and subscriberCircle C1 =NewCircle ( Wu); Rectangle R1=NewRectangle ( A,9); ShapeContainer SC=NewShapeContainer (); //ADD The shapes to the container.SC. AddShape (C1); Sc. AddShape (R1); //cause some events to be raised.C1. Update ( $); R1. Update (7,7); //Keep the console window open in debug mode.System.Console.WriteLine ("Press any key to exit."); System.Console.ReadKey (); } }}/*output:received event. Shape area was now 10201.86 Drawing a circle Received event. Shape area are now Drawing a rectangle*/
Reference
How To:raise Base Class Events in Derived Classes (C # Programming Guide)
C # Foundation-Event inheritance class cannot directly raise events of base class