#1,162 – Virtual Events Support Polymorphism

In C#, polymorphism is implemented using virtual members–which can be methods, properties, indexers or events.

A virtual event has an implementation in the base class that can be overridden in a derived class.  When the event’s add or remove accessors are invoked, the specific accessor used is determined at run-time based on the type of the underlying object.

A virtual event is defined in the base class using the virtual keyword.

        // Code from Dog class
        private EventHandler barked;
        public virtual event EventHandler Barked
        {
            add
            {
                barked += value;
                Console.WriteLine("Dog.Barked add accessor");
            }
            remove
            {
                barked -= value;
                Console.WriteLine("Dog.Barked remove accessor");
            }
        }

A virtual event is overridden in a derived class using the override keyword.

        // Code from Terrier : Dog class
        private EventHandler myBarked;
        public override event EventHandler Barked
        {
            add
            {
                myBarked += value;
                Console.WriteLine("Terrier.Barked add accessor");
            }
            remove
            {
                myBarked -= value;
                Console.WriteLine("Terrier.Barked remove accessor");
            }
        }

Using the event:

            Dog d = new Dog("Bob");
            d.Barked += (s, e) => { Console.WriteLine("Dog.Barked"); };

            Terrier t = new Terrier("Jack");
            t.Barked += (s, e) => { Console.WriteLine("Terrier.Barked"); };

            Dog d2 = t;
            d2.Barked += (s, e) => { Console.WriteLine("Terrier.Barked"); };

1162-001

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

4 Responses to #1,162 – Virtual Events Support Polymorphism

  1. Ian says:

    Hello, I wonder if you could help me understand how virtual methods are implemented, I thought I understood but can’t work out the solution below:
    using System.IO;
    using System;

    class Program
    {
    static void Main() {
    D d = new D();
    A a = d;
    B b = d;
    C c = d;
    a.F();
    b.F();
    c.F();
    d.F();
    }

    class A
    {
    public virtual void F() { Console.WriteLine(“A.F”); }
    }
    class B: A
    {
    public override void F() { Console.WriteLine(“This is B.F”); }
    }
    class C: B
    {
    new public virtual void F() { Console.WriteLine(“C.F”); }
    }
    class D: C
    {
    public override void F() { Console.WriteLine(“D.F”); }
    }
    }

    ————-Output—————–
    B.F
    B.F
    D.F
    D.F
    —————————————-
    I would of expected
    D.F
    B.F
    D.F
    D.F
    as a results.
    I don’t understand how (a) got anything from (b).
    I’m trying to think of it on the heap, (abc) are just references to object d?

    Any help would be great thank you.

    like your site by the way.

  2. Steven says:

    Ian – D derived C. C.F function is not polymophically due to the new key word you used in that method.

Leave a comment