all groups > dotnet clr > september 2007 >
You're in the

dotnet clr

group:

unitialized readonly fields produces no warning



Re: unitialized readonly fields produces no warning Peter Duniho
9/29/2007 4:37:54 PM
dotnet clr: [quoted text, click to view]

It's not uninitialized, that's why.

For class members, there's a well-defined initialization, using the
default value for the variable.

[quoted text, click to view]

Same thing as above. The variable is initialized, so emitting a warning
saying that it's not initialized would be incorrect.

unitialized readonly fields produces no warning cody
9/29/2007 9:11:20 PM
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
AddThis Social Bookmark Button