Sunday, September 24, 2006 3:39 PM bart

C# Quiz - 'virtual', 'new' and 'override'

Introduction

Normally I don't post quizzes over here, but time for a little exception (no prizes to win). A few days ago I was talking to someone who was a little confused on the field of polymorfic constructions in C# (and more general, polymorphism as such). There are three keywords in C# that play prominent roles in this context: virtual, new and override.

Quiz

What will be the output of the following piece of code?

using System;

class
Ns
{
   public static void
Main()
   {
      B b =
new
B();
      b.Bar();
      b.Foo();

      A a = b;
      a.Bar();
      a.Foo();
   }

   class
A
   {
      public virtual void
Bar()
      {
         Console.WriteLine("A.Bar"
);
      }

      public virtual void
Foo()
      {
         Console.WriteLine("A.Foo"
);
      }
   }

   class
B : A
   {
      public override void
Bar()
      {
         Console.WriteLine("B.Bar"
);
      }

      public new void
Foo()
      {
         Console.WriteLine("B.Foo"
);
      }
   }
}

It's remarkable how much people don't know about the new keyword in this context. And maybe that's best, because hiding is in my opinion a somewhat ugly feature that conflicts with object-oriented design.

Extra questions:

  1. Drop the override keyword and compile again; what do you see now?
  2. Where can I apply the sealed keyword on class B and it's members?

PS: The guy I was talking to has spent a few years in the world of Java and is relatively new to .NET. Nothing bad about Java of course, but the differences of default behavior (C#'s explicitness of a "virtual" method versus Java's implicitness for virtual methods for instance) in various OO languages can be quite confusing indeed.

I agree, for most people this will be peanuts, but nevertheless I thought it could be valuable to some of you, maybe still climbing the learning curve of .NET.

For those of you who can't get enough of this, check out this post by Gregory Young. About a month ago, this issue was escalated on the MVP C# newsgroup and is currently under investigation at Microsoft. Just to show how OO can lead to real brainteasers (especially when there is some nasty bug that makes you doubt about the completeness of your OO skills :-)).

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under:

Comments

# re: C# Quiz - 'virtual', 'new' and 'override'

Monday, September 25, 2006 11:21 PM by Consultant

[quote]
It's remarkable how much people don't know about the new keyword in this context.
[/quote]

That is remarkable, truely ... I do however contradict your remark as conflicting OO design. The new keyword can be very powerful, I have a few stratigies on entity relationship models, and complex application flow blocks that almost implements AOP style inheritance.

Your quiz was funny though

# re: C# Quiz - 'virtual', 'new' and 'override'

Tuesday, September 26, 2006 12:04 PM by bart

Hi Consultant,

I do agree there are scenarios where the new keyword is appropriate to use; and indeed, if it had no use, it would have made little chance to be included in a pragmatic language like C#. The fact however is that from a pure OO point of view, one says that the method being invoked depends on the type of the object, not the reference being used to point to that object. It this based on this observation, one can label hiding as anti-OO.

But as usual, if you use it with care and know what you're doing, you might be happy to use it.

-Bart