2

Daily question: Fun with constructors

Posted in C#, Daily Question at July 10th, 2008 by Gerrod /

It’s been a while since anyone has posted a daily question, but here’s one to get y’all in the mood…

Given the following code:


public class BaseClass {
    public BaseClass() : this("Base value") {
        Console.WriteLine("Base: Empty");
    }

    public BaseClass(object value) {
        Console.WriteLine("Base with value: {0}", value);
    }
}

public class DerivedClass : BaseClass {
    public DerivedClass() : this("Derived value") {
        Console.WriteLine("Derived: Empty");
    }

    public DerivedClass(object value) : base(value) {
        Console.WriteLine("Derived with value: {0}", value);
    }
}

What will be printed to the console when a new DerivedClass object is created using the default DerivedClass constructor:


    new DerivedClass();

First person to post the correct answer wins a prize consiting solely of that warm fuzzy feeling you get by being the first person to answer a question correctly.

Published in C#, Daily Question

2 Responses to “Daily question: Fun with constructors”

  1. July 11th, 2008 at 2:04 am #Ben

    I just wish I had a compiler :)

    Also, what version of Coldfusion are you using? There’s no such thing as a constructor in MX 6.1?

    I’m guessing this:

    Base with value: Derived value
    Derived with value: Derived value
    Derived: Empty

    But am happy to concede that the execution order for : this(..) is different to : base(…) and thus could be wrong.

  2. July 11th, 2008 at 9:48 am #Gerrod

    Nope, you’re absolutely right Bender. Good one; it was a bit of a trick question!

Leave a Reply