OdeToCode IC Logo

What's Wrong With This Code?

Tuesday, August 1, 2006

The developer who wrote the following code expects to see "Hello World!" printed twice. What will the developer see, and why? (Hint: "Hello, World!" will only appear once).

using System;

class Program
{
    
static void Main()
    {
        
Console.WriteLine(Child.Message);
        
new Child();
        
Console.WriteLine(Child.Message);
    }
}

class Parent
{
    
static Parent()
    {
        _message =
"Hello!";
    }

    
static public string Message
    {
        
get { return _message; }
    }

    
static protected string _message;
}

class Child : Parent
{
    
static Child()
    {
        _message =
"Hello, World!";
    }
}