Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / October 2007

Tip: Looking for answers? Try searching our database.

unitialized readonly fields produces no warning

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cody - 29 Sep 2007 20:11 GMT
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.
Peter Duniho - 30 Sep 2007 00:37 GMT
> 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
Ben Voigt [C++ MVP] - 03 Oct 2007 18:49 GMT
>> It is possible to declare and use/instantiate a class with a
>> uninitialized readonly field without even a compiler warning. Why don't I
[quoted text clipped - 14 lines]
> 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?
Peter Duniho - 03 Oct 2007 19:17 GMT
>>> It is possible to declare and use/instantiate a class with a
>>> uninitialized readonly field without even a compiler warning. Why don't I
[quoted text clipped - 14 lines]
> 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
cody - 08 Oct 2007 21:07 GMT
>>>> It is possible to declare and use/instantiate a class with a
>>>> uninitialized readonly field without even a compiler warning. Why
[quoted text clipped - 47 lines]
>
> 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..".

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.