#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

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: