What if we donβt inherit for some time?
1 Suppose, instead of inheriting from a common base class, create your own client class, which requires an event mechanism with an object that implements an event mechanism.
Assume our class
public class EventNotifier { public event EventHandler OnChange; private bool _hasChanged; public bool HasChanged { get { return _hasChanged; } set { _hasChanged = value;
2 -
public class A { private EventNotifier eventNotifier; public EventNotifier MyEventNotifier { get { return eventNotifier; } } public A() { eventNotifier = new EventNotifier(); } }
3 Now your class A users (class, inherited / composed class A)
it is for if B contains A
public class b { A obj ; public b() { obj = new A(); obj.MyEventNotifier.OnChange += new EventHandler(delegate { Console.WriteLine("Hi"); }); obj. MyEventNotifier.HasChanged = true; } }
source share