#1,164 – A Sealed Event Cannot Be Overridden

As with a method, you can use the sealed keyword when overriding a virtual event to indicate that child classes cannot override the event being defined.

When you override an event, you create a virtual event that behaves polymorphically.  The base class defines the event using the virtual keyword and you define the event in your child class using the override keyword.

When you use the override keyword, your event can itself be overridden in a child class.

If you want to override an event, but prevent any derived classes from overriding your implementation, you can use the sealed keyword to indicate that a child class must use the new keyword, rather than overriding your event.

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

        public Dog(string name)
            Name = name;

        public virtual event EventHandler<string> Barked;

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

        private EventHandler<string> terrierBarked;
        public sealed override event EventHandler<string> Barked
                terrierBarked += value;
                // Special Terrier.Barked event logic here
                terrierBarked -= value;

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

        public new event EventHandler<string> Barked;

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

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: