Skip to content

Instantly share code, notes, and snippets.

@euyuil
Created February 25, 2014 02:54
Show Gist options
  • Save euyuil/9201759 to your computer and use it in GitHub Desktop.
Save euyuil/9201759 to your computer and use it in GitHub Desktop.
C#: event polymorphism test. Avoid 'overriding' events of parent class.
namespace EventTest
{
using System;
class Parent
{
public event Action Event;
public virtual void DoEvent()
{
Console.WriteLine("\tParent DoEvent In");
OnEvent();
Console.WriteLine("\tParent DoEvent Out");
}
protected virtual void OnEvent()
{
Console.WriteLine("\t\tParent OnEvent In");
if (Event == null)
{
Console.WriteLine("\t\t\tParent OnEvent: Event is null");
}
else
{
Event();
}
Console.WriteLine("\t\tParent OnEvent Out");
}
}
// Not right approach: event handlers will be invoked twice.
class Alice : Parent
{
public override void DoEvent()
{
Console.WriteLine("\tAlice DoEvent In");
base.DoEvent();
OnEvent();
Console.WriteLine("\tAlice DoEvent Out");
}
protected override void OnEvent()
{
Console.WriteLine("\t\tAlice OnEvent In");
base.OnEvent();
Console.WriteLine("\t\tAlice OnEvent Out");
}
}
// Not right approach: event handlers will be invoked twice.
class Bob : Parent
{
// Will hide base.Event from outside.
public new event Action Event;
public override void DoEvent()
{
Console.WriteLine("\tBob DoEvent In");
base.DoEvent();
OnEvent();
Console.WriteLine("\tBob DoEvent Out");
}
protected override void OnEvent()
{
Console.WriteLine("\t\tBob OnEvent In");
base.OnEvent();
if (Event == null)
{
Console.WriteLine("\t\t\tBob OnEvent: Event is null");
}
else
{
Event();
}
Console.WriteLine("\t\tBob OnEvent Out");
}
}
// Right approach to 'override' parent's event.
class Charlie : Parent
{
public Charlie()
{
base.Event += BaseEventHandler;
}
// Will hide base.Event from outside.
public new event Action Event;
public override void DoEvent()
{
Console.WriteLine("\tCharlie DoEvent In");
base.DoEvent();
OnCharlieEvent();
Console.WriteLine("\tCharlie DoEvent Out");
}
private void BaseEventHandler()
{
Console.WriteLine("\t\t\tCharlie Knows Base Event Triggered");
}
// Prevent overriding OnEvent in descendant classes.
protected override sealed void OnEvent()
{
base.OnEvent();
}
protected virtual void OnCharlieEvent()
{
Console.WriteLine("\t\tOnCharlieEvent In");
if (Event == null)
{
Console.WriteLine("\t\t\tOnCharlieEvent: Event is null");
}
else
{
Event();
}
Console.WriteLine("\t\tOnCharlieEvent Out");
}
}
class Program
{
static void Main(string[] args)
{
var p = new Parent();
var a = new Alice();
var b = new Bob();
var c = new Charlie();
Action peh = () => Console.WriteLine("\t\t\tInvoke Parent Event Handler");
Action aeh = () => Console.WriteLine("\t\t\tInvoke Alice Event Handler");
Action beh = () => Console.WriteLine("\t\t\tInvoke Bob Event Handler");
Action ceh = () => Console.WriteLine("\t\t\tInvoke Charlie Event Handler");
p.Event += peh;
Console.WriteLine("Invoking Parent DoEvent");
p.DoEvent();
Console.WriteLine("Invoked Parent DoEvent");
a.Event += aeh;
Console.WriteLine("Invoking Alice DoEvent");
a.DoEvent();
Console.WriteLine("Invoked Alice DoEvent");
b.Event += beh;
Console.WriteLine("Invoking Bob DoEvent");
b.DoEvent();
Console.WriteLine("Invoked Bob DoEvent");
c.Event += ceh;
Console.WriteLine("Invoking Charlie DoEvent");
c.DoEvent();
Console.WriteLine("Invoked Charlie DoEvent");
}
}
}
Invoking Parent DoEvent
Parent DoEvent In
Parent OnEvent In
Invoke Parent Event Handler
Parent OnEvent Out
Parent DoEvent Out
Invoked Parent DoEvent
Invoking Alice DoEvent
Alice DoEvent In
Parent DoEvent In
Alice OnEvent In
Parent OnEvent In
Invoke Alice Event Handler
Parent OnEvent Out
Alice OnEvent Out
Parent DoEvent Out
Alice OnEvent In
Parent OnEvent In
Invoke Alice Event Handler
Parent OnEvent Out
Alice OnEvent Out
Alice DoEvent Out
Invoked Alice DoEvent
Invoking Bob DoEvent
Bob DoEvent In
Parent DoEvent In
Bob OnEvent In
Parent OnEvent In
Parent OnEvent: Event is null
Parent OnEvent Out
Invoke Bob Event Handler
Bob OnEvent Out
Parent DoEvent Out
Bob OnEvent In
Parent OnEvent In
Parent OnEvent: Event is null
Parent OnEvent Out
Invoke Bob Event Handler
Bob OnEvent Out
Bob DoEvent Out
Invoked Bob DoEvent
Invoking Charlie DoEvent
Charlie DoEvent In
Parent DoEvent In
Parent OnEvent In
Charlie Knows Base Event Triggered
Parent OnEvent Out
Parent DoEvent Out
OnCharlieEvent In
Invoke Charlie Event Handler
OnCharlieEvent Out
Charlie DoEvent Out
Invoked Charlie DoEvent
@ambreenmoeen
Copy link

No you can not do this test in South Africa since molecule estimate (LDL) isn't accessible in South Africa right now. You can do it in different countires and before it you should have all data in regards to LDL plainly. Generally it's unrealistic to pass it. Presently here I attempt to discover extraordinary compared to other sites like paperwritingpro.com to get help and in the event that anybody have thought regarding that place at that point must inform me concerning it. It will be incredible for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment