C
cody
It is possible to declare and use/instantiate a class with a
uninitialized readonly field without even a compiler warning. Why don't
I get warnings?
public class Stuff
{
public readonly int a;
}
By definition, readonly fields can only be initialized inside a
constructor or within the declaration of the field, so the compiler
should know that it is a programming mistake here.
I would even say that also for normal class fields there should be a
test that if nowhere in the class an assignment to that variable is
found, a warning should be produced. Of course this only applies only to
private and internal variables, otherwise also code outside the assembly
could initialize the variable which cannot be determined at compile time.
uninitialized readonly field without even a compiler warning. Why don't
I get warnings?
public class Stuff
{
public readonly int a;
}
By definition, readonly fields can only be initialized inside a
constructor or within the declaration of the field, so the compiler
should know that it is a programming mistake here.
I would even say that also for normal class fields there should be a
test that if nowhere in the class an assignment to that variable is
found, a warning should be produced. Of course this only applies only to
private and internal variables, otherwise also code outside the assembly
could initialize the variable which cannot be determined at compile time.