#1,160 – Implementing Events Defined in an Interface

A class that implements an interface needs to define any events that are part of the interface.  Below is an example.

    public interface IDogEvents
    {
        event EventHandler Barked;
        event EventHandler Escaped;
    }

    public class Dog : IDogEvents
    {
        public string Name { get; set; }
        public bool IsPresent { get; set; }

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

        public void Bark()
        {
            Console.WriteLine("Woof");
            if (IsPresent)
                Barked(this, EventArgs.Empty);
            else
                Escaped(this, EventArgs.Empty);
        }

        public event EventHandler Barked = delegate { };

        public event EventHandler Escaped = delegate { };
    }