Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 181071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:41:49+00:00 2026-05-11T14:41:49+00:00

Below is the program I used for the test. It prints (as expected): Raise

  • 0

Below is the program I used for the test. It prints (as expected):

Raise A Event from A Raise B Event from B 

Now, if we change first two lines of the Main to be:

        A a = new B();         B b = new B(); 

the Program will print:

Raise A Raise B Event from B 

which is also expected, as overriding event hides the private backing field in the base class and therefore events fired by the base class are not visible to clients of the derived class.

Now I am changing the same lines to:

 B b = new B();  A a = b; 

and the program starts printing:

Raise A Raise B Event from A Event from B 

What’s going on?

class A {     public virtual event EventHandler VirtualEvent;     public void RaiseA()     {         Console.WriteLine('Raise A');         if (VirtualEvent != null)         {             VirtualEvent(this, EventArgs.Empty);         }     } } class B : A {     public override event EventHandler VirtualEvent;     public void RaiseB()     {         Console.WriteLine('Raise B');                      if (VirtualEvent != null)         {             VirtualEvent(this, EventArgs.Empty);         }     } }  class Program {     static void Main(string[] args)     {         A a = new A();         B b = new B();          a.VirtualEvent += (s, e) => Console.WriteLine('Event from A');         b.VirtualEvent += (s, e) => Console.WriteLine('Event from B');          a.RaiseA();         b.RaiseB();     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:41:50+00:00Added an answer on May 11, 2026 at 2:41 pm

    We have a single instance (of B) which has the following fields:

    • A.VirtualEvent: null
    • B.VirtualEvent: Two event handlers

    The call to a.RaiseA() just prints ‘Raise A’ – but nothing more, because the private field in A is null.

    The call to b.RaiseB() prints the remaining three lines, because the event has been subscribed to twice (once to print ‘Event from A’ and once to print ‘Event from B’).

    Does that help?

    EDIT: To make it clearer – think of the virtual event as a pair of virtual methods. It’s very much like this:

    public class A {     private EventHandler handlerA;      public virtual void AddEventHandler(EventHandler handler)     {         handlerA += handler;     }      public virtual void RemoveEventHandler(EventHandler handler)     {         handlerA -= handler;     }      // RaiseA stuff }  public class B : A {     private EventHandler handlerB;      public override void AddEventHandler(EventHandler handler)     {         handlerB += handler;     }      public override void RemoveEventHandler(EventHandler handler)     {         handlerB -= handler;     }      // RaiseB stuff } 

    Now is it clearer? It’s not quite like that because as far as I’m aware you can’t override just ‘part’ of an event (i.e. one of the methods) but it gives the right general impression.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 96k
  • Answers 96k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes it is legal You don't say what the problem… May 11, 2026 at 7:07 pm
  • Editorial Team
    Editorial Team added an answer you could knock it up with pyBison and friends using… May 11, 2026 at 7:07 pm
  • Editorial Team
    Editorial Team added an answer In order to edit external stylesheets with JS you'll need… May 11, 2026 at 7:07 pm

Related Questions

I'm using boost for several C++ projects. I recently made a upgrade (1.33.1 to
Is it possible to open a file in .NET with non exclusive write access?
SOLVED I am using delphi 2009. My program listens for usb drives being connected
Am I doing something wrong or is it not possible to specify a generic

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.