#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 { };
    }

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

2 Responses to #1,160 – Implementing Events Defined in an Interface

  1. Pingback: Dew Drop – August 14, 2014 (#1836) | Morning Dew

  2. Pingback: Dew Drop – August 19, 2014 (#1837) | Morning Dew

Leave a comment