What's Wrong With This Code?

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!";
    }
}

posted on Monday, July 31, 2006 10:30 PM by scott

Comments

Monday, July 31, 2006 8:04 PM by Joshua Flanagan

# re: What's Wrong With This Code?

Because for whatever reason, the C# compiler lets you reference static properties/methods using a descendent class name, even though it has nothing to do with the descendent class.

The "hello world" message isn't set until the Child static constructor is called, which isn't until the Child class is first used. The first call to Child.Message doesn't really reference the Child class - it is a straight passthrough to the Parent class.

This "feature" leads to so much confusion (this isn't the first quiz I've seen that takes advantage of the fact), it really should be a compiler error.
Monday, July 31, 2006 8:11 PM by scott

# re: What's Wrong With This Code?

Correct, Joshua!
Tuesday, August 01, 2006 7:35 AM by sergio giraldo

# re: What's Wrong With This Code?

The devious beforefieldinit metadata flag ?
Tuesday, August 01, 2006 9:13 AM by RedBull

# re: What's Wrong With This Code?

This seems like a bug; the first line references the Child class and so should call the static constructor for Child.
Tuesday, August 01, 2006 10:42 AM by SepzonE

# Inheritence - Static Parent Child Classes

I don't claim to be a .Net expert (not yet anyways)! So when I came across this post by K Scott Allen,
Tuesday, August 01, 2006 8:32 PM by Christopher Steen

# Link Listing - August 1, 2006


Fiddler 1.2 (now with font sizes!) [Via: stevem@hyperthink.net ]
ASP.NET Podcast Show #60 - Atlas...
Wednesday, August 02, 2006 5:59 AM by Mark Lindell

# re: What's Wrong With This Code?

I believe a rule in code analysis will catch this. Study the CA rules! There is tons of stuff to learn in there.
Wednesday, August 02, 2006 8:04 AM by SepzonE

# Inheritence - Static Parent Child Classes

I don't claim to be a .Net expert (not yet anyways)! So when I came across this post by K Scott Allen,
Thursday, August 10, 2006 1:08 AM by jokiz's blog

# what's wrong with this code series

scottallen has started a "what's wrong with this code" series. some items are trivial and
Wednesday, August 16, 2006 2:05 AM by Dan

# re: What's Wrong With This Code?

"Because for whatever reason, the C# compiler lets you reference static properties/methods using a descendent class name, even though it has nothing to do with the descendent class."

I believe the reason is to enable you to make static methods "virtual".

Ive used it myself to provide a null-insensitive ToString static method in a base class, and to be able to override the default implementation easilly in some places.