I've been both scratching and banging my head on this one. I suspect I;m just being daft here, but I can't seem to get a const, or static, member to initialize so I can use it throughout a class.
Here's an example that demonstrates my problem (or rather my misunderstanding):
using System;
namespace ConstExample
{
public class HasImmutableMember
{
// static private double fSectionLengthTolerancePerInch = 1 / (20 * 12); // tolerance is 1" per every 20'
private const double fSectionLengthTolerancePerInch = 1 / (20 * 12); // tolerance is 1" per every 20'
static HasImmutableMember()
{
Console.WriteLine("static c'tor: " + fSectionLengthTolerancePerInch);
}
public HasImmutableMember()
{
Console.WriteLine("instance c'tor: " + fSectionLengthTolerancePerInch);
}
}
public class Program
{
public void Main(string[] args)
{
HasImmutableMember instance = new HasImmutableMember();
}
}
}
Console output is:
static c'tor: 0
instance c'tor: 0
Can't set a breakpoint at the const member declaration, but if I use the static version, I can. Both fail to give me what I want. The static member declaration does get hit prior to either the static c'tor or the instance c'tor. And as expected the static c'tor gets hit before the instance c'tor. But in both the static and instance c'tor's the value of my member is 0 rather than the initialized value.
What am I missing?