Exercises in this lecture   previous -- Keyboard shortcut: 'p'        Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home      

Exercise 7.9
Non-virtual variables - Virtual Methods

Take a look at the following program

using System;

public class A {
 public int v = 1;
 public virtual int Op1(){return v;}

public class B: A{
 public new int v = 2;
 public override int Op1(){return v;}
 public int Op2(){return base.v;}

public class Client{

  public static void Main (){
    A a = new B();
    Console.WriteLine("{0}", a.v);
    Console.WriteLine("{0}", a.Op1());

    B b = new B();
    Console.WriteLine("{0}", b.v);
    Console.WriteLine("{0}", b.Op2());


and figure out what it prints.

Explain the behaviour.