By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,733 Members | 1,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,733 IT Pros & Developers. It's quick & easy.

unitialized readonly fields produces no warning

P: n/a
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.
Sep 29 '07 #1
Share this Question
Share on Google+
4 Replies

P: n/a
cody wrote:
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?
It's not uninitialized, that's why.

For class members, there's a well-defined initialization, using the
default value for the variable.
[...]
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.
Same thing as above. The variable is initialized, so emitting a warning
saying that it's not initialized would be incorrect.

Pete
Sep 29 '07 #2

P: n/a

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
cody wrote:
>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?

It's not uninitialized, that's why.

For class members, there's a well-defined initialization, using the
default value for the variable.
>[...]
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.

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

With C#, you'd typically get a warning alow the lines of "variable xyz is
never written and will always have its default value null".

Or is it assigned in some constructors and not others?
Oct 3 '07 #3

P: n/a
Ben Voigt [C++ MVP] wrote:
"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
>cody wrote:
>>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?

It's not uninitialized, that's why.
[...]
>>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. [...]

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

Which language?

With C#, you'd typically get a warning alow the lines of "variable xyz is
never written and will always have its default value null".
That's not a warning saying that the variable is uninitialized. It's
saying that it _is_ initialized, to the default value.

It's true, I should have read the original post more carefully. The
author seems to be saying that NO warning is provided, while I
interpreted it as saying that there's no warning about the uninitialized
variable.

There definitely is a warning, assuming the read-only field is not
explicitly assigned anywhere. It's just not saying that the variable is
uninitialized.

IMHO, the OP confuses the issue by including the second scenario, which
has even more reason for not generating any warning. But certainly even
in that case too, a warning about an uninitialized variable is unwarranted.
Or is it assigned in some constructors and not others?
Having a constructor that initializes a readonly field will suppress the
"default value" warning, yes. After all, the warning is simply saying
that there's _no_ way for the field to be anything other than the
default value. The lack of a warning doesn't mean that the field will
_always_ be something other than the default value.

But the warning is talking about the value that _is_ used to initialize
the field, not whether the field is initialized at all.

Perhaps this is the case the OP is running into in which _no_ warnings
are produced. But even if a warning is produced, it won't be one that
mentions an uninitialized field.

Pete
Oct 3 '07 #4

P: n/a
Peter Duniho wrote:
Ben Voigt [C++ MVP] wrote:
>"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
>>cody wrote:
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?

It's not uninitialized, that's why.
[...]
>>>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. [...]

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

Which language?

With C#, you'd typically get a warning alow the lines of "variable xyz
is never written and will always have its default value null".

That's not a warning saying that the variable is uninitialized. It's
saying that it _is_ initialized, to the default value.

It's true, I should have read the original post more carefully. The
author seems to be saying that NO warning is provided, while I
interpreted it as saying that there's no warning about the uninitialized
variable.

There definitely is a warning, assuming the read-only field is not
explicitly assigned anywhere. It's just not saying that the variable is
uninitialized.

IMHO, the OP confuses the issue by including the second scenario, which
has even more reason for not generating any warning. But certainly even
in that case too, a warning about an uninitialized variable is unwarranted.
>Or is it assigned in some constructors and not others?

Having a constructor that initializes a readonly field will suppress the
"default value" warning, yes. After all, the warning is simply saying
that there's _no_ way for the field to be anything other than the
default value. The lack of a warning doesn't mean that the field will
_always_ be something other than the default value.

But the warning is talking about the value that _is_ used to initialize
the field, not whether the field is initialized at all.

Perhaps this is the case the OP is running into in which _no_ warnings
are produced. But even if a warning is produced, it won't be one that
mentions an uninitialized field.

Pete
My problem is that there is *no* warning at all, even if it is
guaranteed that the variable is never assigned to in the program:

public class B
{
public readonly int a;
}

static void Main()
{

B b = new B();
int i = b.a;
}

It also doesn't matter whether the class is sealed or not or whether it
has an internal constructor. There is *no* warning - unless "a" itself
is declared internal or private in which case a warning is generated
that "a is never assigned to..".
Oct 8 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.