Consider the following code:
class Program
{
static Program() {
Program.program1.Value = 5;
}
static List<Program> values = new List<Program>();
int value;
int Value
{
get { return value; }
set {
this.value = value;
Program.values.Add(this);
}
}
static Program program1 = new Program { value = 1 };
static Program program2 = new Program { value = 2 };
static Program program3 = new Program { value = 3 };
static void Main(string[] args)
{
if (Program.values.Count == 0) Console.WriteLine("Empty");
foreach (var value in Program.values)
Console.WriteLine(value.Value);
Console.ReadKey();
}
}
It prints only the number 5, and if removed the code in the static constructor, it prints “Empty”.
Is there a way to force static fields to be initialized even whether not used yet?
I need to have a static property named Values with returns all instances of the referred type.
I tried some variations of this code and some works for some types but doesn’t for others.
EDIT: THE SAMPLE ABOVE IS BROKEN, TRY THIS ONE:
class Subclass<T> {
static Subclass()
{
Values = new List<Subclass<T>>();
}
public Subclass()
{
if (!Values.Any(i => i.Value.Equals(this.Value)))
{
Values.Add(this);
}
}
public T Value { get; set; }
public static List<Subclass<T>> Values { get; private set; }
}
class Superclass : Subclass<int>
{
public static Superclass SuperclassA1 = new Superclass { Value = 1 };
public static Superclass SuperclassA2 = new Superclass { Value = 2 };
public static Superclass SuperclassA3 = new Superclass { Value = 3 };
public static Superclass SuperclassA4 = new Superclass { Value = 4 };
}
class Program
{
static void Main(string[] args)
{
//Console.WriteLine(Superclass.SuperclassA1); //UNCOMMENT THIS LINE AND IT WORKS
foreach (var value in Superclass.Values)
{
Console.WriteLine(value.Value);
}
Console.ReadKey();
}
}
The answer to your question is ‘well, yes’. But one of the two ways of "forcing" it is what you’re already doing.
The relevant section in the language spec is Static constructors, and specifically: