vooose <no****@microsoft.com> wrote:
Consider
{
bool var;
if(var)
Console.WriteLine("var is true");
}
results in the compilation error
Use of unassigned local variable 'var'
If you make var an instance variable you don't get a compile error as
the variable is effectively 'initialized' to all zeros. Why can't local
variables behave in this manner?
They could - but it's better that they don't. In a way, the *downside*
is that you *can't* do this with instance variables.
The fact that you can't accidentally use a local variable before it's
been initialised is great IMO. I've seen plenty of bugs in C code which
does just that.
It irritates me that I have to type
bool var=false;
If you want to just not execute some code, don't include it in the
first place, or write:
if (false)
It's relatively rare that I find I have to initialise a local variable
with a value which isn't actually useful.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too