2007/11/21

Brainstorming: An interested interview question

I saw this code segment on a blog:
public class A
{
  void PrintA()
  {
    Console.WriteLine("Print A");
  }

  class B : Program
  {
    new void PrintA()
    {
      Console.WriteLine("Print B");
    }
  }

  public static void Main()
  {
    A Atemp = new A();
    B Btemp = new B();

    Atemp.PrintA();
    Btemp.PrintA();
  }
}
What do you think the result will be? Don't forget this is a Interview question, that means you have to answer no more than few minutes.

The answer is:
Print A
Print A
Why not and how to get the following result?
Print A
Print B

The key point is the inner/nested class. We have to declare it is a public class so it's visible to everyone.

No comments:

Post a Comment