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

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 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: