I have the following class:
public class AssignmentStatusCode
{
public static AssignmentStatusCode Pending { get; } = new AssignmentStatusCode("P");
public static AssignmentStatusCode Rejected { get; } = new AssignmentStatusCode("R");
public static AssignmentStatusCode Approved { get; } = new AssignmentStatusCode("A");
public static implicit operator string(AssignmentStatusCode assignmentStatusCode)
{
return assignmentStatusCode.Value;
}
private static readonly HashSet<string> ValidStatusCodes = new HashSet<string>(new[] { "A", "R", "P" });
public AssignmentStatusCode(string value)
{
if (!ValidStatusCodes.Contains(value))
{
throw new ArgumentOutOfRangeException(nameof(value),
$"Value must be {string.Join(", ", ValidStatusCodes.Select(c => $"'{c}'"))}.");
}
Value = value;
}
public string Value { get; }
}
When I create an instance of this class using var a = new AssignmentStatusCode("A")
, a NullReferenceException
is thrown on the if
check of the instance constructor. Debugging indicates that ValidStatusCodes
is null
.
ValidStatusCodes
has a static initializer on it.
According to the C# spec:
If a static constructor (§10.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class.
Why isn't my static field getting initialized before it is being accessed in the constructor? I have a feeling that there is something very simple that I'm glossing over, but I've spent too much time debugging this with no progress; it's time to ask for help.
Apparently, if I had read the spec more closely, I would have noticed this at the beginning of the paragraph I quoted above, which was the root of my problem.
10.5.5.1 Static field initialization The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. If a static constructor (§10.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class
Thank you all for your help.