#1,163 – An Abstract Event Has No Implementation

An abstract event is a special kind of virtual event, defined in a base class, which has no implementation of its own.  A derived class must define every abstract event (as well as every other abstract member) using the override keyword.

    public abstract class Dog 
    {
        public string Name { get; set; }

        public Dog(string name)
        {
            Name = name;
        }

        public abstract event EventHandler<string> Barked;

        public virtual void Bark()
        {
            Console.WriteLine("Woof");
        }
    }

    public class Terrier : Dog
    {
        public Terrier(string name) : base(name) { }

        public override event EventHandler<string> Barked;

        public override void Bark()
        {
            Console.WriteLine("Yip yip");
            Barked(this, "Yip yip");
        }
    }

The class in which the abstract event is defined must be defined as an abstract class, using the abstract keyword.

A derived class may not hide the abstract event in the base class, using the new keyword.  It must implement the inherited abstract event and the new event must be virtual, defined using the override keyword.

Example of using this event:

            Terrier t = new Terrier("Jack");
            t.Barked += (o, s) => Console.WriteLine("- " + s + " -");
            t.Bark();

1163-001

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a comment