The following code compiles with warning and intellisense error:
ref class Test {
initonly static TimeSpan Delay = TimeSpan(1,1,1);
Test() {
long long ticks = Delay.Ticks; // << problem
}
};
The problems are:
How to access Ticks properly?
Well, that's pretty major fail-whale. The warning is accurate, the compiler doesn't know enough about the TimeSpan::Tick property getter. It cannot ensure that the getter doesn't do anything that might alter the value of the struct and thereby invalidates the initonly contract. It solves it by making a copy of the struct and warns about it since this is a possible perf issue. That's a bit heavy-handed, other managed compilers make the copy without saying anything about it. I'd just plunk a #pragma warning(disable:4395)
ahead of it so the warning is suppressed.
The IntelliSense error only appears when I try this in VS2012. That's a bug. You can report it at connect.microsoft.com.
Given the misery and that this appears to be a private class member, I'd just drop initonly to get ahead. If that's not desirable then you can wrap the field with a property, intentionally creating the copy, and thus get rid of both problems, like this:
ref class Test {
initonly static TimeSpan _delay = TimeSpan(1,1,1);
static property TimeSpan Delay {
TimeSpan get() { return _delay; }
}
Test() {
long long ticks = Delay.Ticks;
}
};
The jitter optimizer will get rid of it so don't worry about overhead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With