473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unitialized readonly fields produces no warning

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
4 2140
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

"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:13******** *****@corp.supe rnews.com...
cody wrote:
>It is possible to declare and use/instantiate a class with a
uninitialize d 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
Ben Voigt [C++ MVP] wrote:
"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:13******** *****@corp.supe rnews.com...
>cody wrote:
>>It is possible to declare and use/instantiate a class with a
uninitializ ed 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
Peter Duniho wrote:
Ben Voigt [C++ MVP] wrote:
>"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:13******* ******@corp.sup ernews.com...
>>cody wrote:
It is possible to declare and use/instantiate a class with a
uninitialize d 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7210
by: Fred K. | last post by:
Hi, I have form text fields that have the attribute readonly. I wish to use a javascript (invoked by a button on my page) to erase the values and set the readonly attribute to false. How do I do this? Here's what I have that is not working with respect to setting readonly to false: function nsfields ( bDefaults ) {
2
4171
by: Ronny Sigo | last post by:
Hello all, As one of a few ways to let a user bring in data into my database I have made 3 excel files. Once the data are in the excel files my program checks if a required value (Taal (Dutch) meaning Language) has been brought in. This value can only have one of two values : N (for Nederlands - Dutch) and E (for English) If a language in...
10
3845
by: William Stacey | last post by:
I know the following is not allowed, but shouldn't it be? sharedObject is part of Derived and should be able to be set in the constructor - no? tia public abstract class Base1 { protected readonly object sharedObject; } public class Derived : Base1 {
19
7697
by: cody | last post by:
Iam wondering what the benefit of using const over static readonly is. static readonly is a runtime constant and can be set once in the initializer or the static ctor, whereas const is suffering from binary incompatibility since it is hardbaked into the binary. I do not believe there is a performance advantage with using const over static...
11
1663
by: Christof Nordiek | last post by:
Hi all, Can there be a performance difference, if i use readonly fields instead of constants? e.g.: const int n = 100; vs. static readonly int = 100;
2
3545
by: Amit | last post by:
Hello, I have a class where I store a lot of setting names. Right now they're all declared as consts : public const CurDoc as string = "CURRENT_DOCUMENT" From what I know about const, every call to CurDoc will be replaced with the text "CURRENT_DOCUMENT" during compilation. Is this right? If yes then wouldn't it be better to declare it...
10
7846
by: sunil | last post by:
Hello, I am new to c# . I have some basic programming doubts. Please help me in clarifying these doubts. I want to initialize a static and readonly field with a value returned by a static method. How ever, when I am debugging, that method is not being called. So I am not able to figure out, whether the field is getting initialized properly...
0
986
by: cody | last post by:
It is possible to declare a class with a uninitialized readonly field without compiler warning. Is this intended and if so, why? public class Stuff { public readonly int a; } By definition, readonly fields can only be initialied inside a constructor or within the declaration of the field.
3
5282
by: =?Utf-8?B?TWlrZSBE?= | last post by:
I need to dynamically set the ReadOnly value in a BoundField in a DetailsView. The DetailsView is generated from a GridView selection and has both Edit and New buttons. I need to be able to set the readonly property on one of the fields depending on the user's group. I have tried to access the property by Detailsview1.Fields.Readonly and by...
0
7479
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7926
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7439
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5987
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.