R
Ryan Steckler
I found this behavior while trying to implement a singleton class. Below is
a somewhat more straight forward example, though admittedly less useful in
real life. The basic problem is that when a static property of a class is
called (StaticInit.MyProperty), the property code executes BEFORE the static
members are initialized. This would lead one to assume that the static
members would equate to null, but that isn't the case either. They are in
some limbo state of uninitialization. Below is a simple class showing the
problem:
Ryan Steckler
Senscom, Inc.
using System;
//This class shows a bug in the language/framework/runtime?
//Apparently, calling a static property does not initialize static members
first. Static members do not equate
//to null, but throw null pointer exceptions when accessed.
//This can be shown by calling this class via:
//int n = StaticInit.MyProperty; //Broken.
//and
//int n = StaticInit.GetMyProperty(); //Works (if called *before* the
preceeding line).
namespace staticinit
{
public class StaticInit
{
//This Arraylist should be initialized to null (<undefined value>)
by the runtime.
private static System.Collections.ArrayList m_al; //adding a " =
null" makes no difference.
public StaticInit()
{
}
public static int MyProperty
{
get
{
if (m_al == null)
{
//The code *never* reaches this point.
m_al = new System.Collections.ArrayList();
}
//Accessing m_al at this point throws a null pointer
exception.
return 1;
}
}
public static int GetMyProperty()
{
if (m_al == null)
{
//The code comes in here the first time GetMyProperty is
executed, as expected.
//UNLESS someone called StaticInit.MyProperty before this
call. In that case, we don't come in here
//and references to m_al *still* result in null pointer
exceptions.
m_al = new System.Collections.ArrayList();
}
return 1;
}
}
}
a somewhat more straight forward example, though admittedly less useful in
real life. The basic problem is that when a static property of a class is
called (StaticInit.MyProperty), the property code executes BEFORE the static
members are initialized. This would lead one to assume that the static
members would equate to null, but that isn't the case either. They are in
some limbo state of uninitialization. Below is a simple class showing the
problem:
Ryan Steckler
Senscom, Inc.
using System;
//This class shows a bug in the language/framework/runtime?
//Apparently, calling a static property does not initialize static members
first. Static members do not equate
//to null, but throw null pointer exceptions when accessed.
//This can be shown by calling this class via:
//int n = StaticInit.MyProperty; //Broken.
//and
//int n = StaticInit.GetMyProperty(); //Works (if called *before* the
preceeding line).
namespace staticinit
{
public class StaticInit
{
//This Arraylist should be initialized to null (<undefined value>)
by the runtime.
private static System.Collections.ArrayList m_al; //adding a " =
null" makes no difference.
public StaticInit()
{
}
public static int MyProperty
{
get
{
if (m_al == null)
{
//The code *never* reaches this point.
m_al = new System.Collections.ArrayList();
}
//Accessing m_al at this point throws a null pointer
exception.
return 1;
}
}
public static int GetMyProperty()
{
if (m_al == null)
{
//The code comes in here the first time GetMyProperty is
executed, as expected.
//UNLESS someone called StaticInit.MyProperty before this
call. In that case, we don't come in here
//and references to m_al *still* result in null pointer
exceptions.
m_al = new System.Collections.ArrayList();
}
return 1;
}
}
}