value types and automaticaly initialized

T

Tony Johansson

Hi!

The variable myInstanceInt in class Foo below is automaticaly initialized to
0 because the implicit constructor that is called.

class Foo
{
int myInstanceInt;

public void MyMethod()
{
int myLocalInt;
Console.WriteLine(myLocalInt);
}
}

The local variable myLocalInt in method MyMethod is not automaticaly
initialized becuase if this valiable is not exlicit
initialized I get compile error when I try to write it's value as in this
statement Console.WriteLine(myLocalInt);

I mean when instance value type like myInstanceInt above is automaticaly
initialized why is not
the local valiable myLocalInt in the method automaticaly initialized ?

//Tony
 
A

Arne Vajhøj

Hi!

The variable myInstanceInt in class Foo below is automaticaly initialized to
0 because the implicit constructor that is called.

class Foo
{
int myInstanceInt;

public void MyMethod()
{
int myLocalInt;
Console.WriteLine(myLocalInt);
}
}

The local variable myLocalInt in method MyMethod is not automaticaly
initialized becuase if this valiable is not exlicit
initialized I get compile error when I try to write it's value as in this
statement Console.WriteLine(myLocalInt);

I mean when instance value type like myInstanceInt above is automaticaly
initialized why is not
the local valiable myLocalInt in the method automaticaly initialized ?

Because the C# language specified it that way.

Why? Invite Anders Hejlsberg for dinner and ask him !

(when a language is created decisions are made)

See below for a bunch of quotes from the C# spec.

Arne

=======================

12.2 Default values
The following categories of variables are automatically initialized to
their default values:
• Static variables
• Instance variables of class instances
• Array elements
The default value of a variable depends on the type of the variable and
is determined as follows:
• For a variable of a value-type, the default value is the same as the
value computed by the value-type’s
default constructor (§11.1.2).
• For a variable of a reference-type, the default value is null.

....

12.3.1 Initially assigned variables
The following categories of variables are classified as initially assigned:
• Static variables
• Instance variables of class instances
• Instance variables of initially assigned struct variables
• Array elements
• Value parameters
• Reference parameters
• Variables declared by a catch clause, a foreach statement, or a using
statement.
12.3.2 Initially unassigned variables
The following categories of variables are classified as initially
unassigned:
• Instance variables of initially unassigned struct variables.
• Output parameters, including the this variable of struct instance
constructors without a constructor
initializer.
• Local variables, except those declared in a catch clause, a foreach
statement, or a using statement.
 
P

Peter Duniho

Tony said:
Hi!

The variable myInstanceInt in class Foo below is automaticaly initialized to
0 because the implicit constructor that is called.

Technically, not true. Member fields are initialized to their default
values no matter what constructor is called, and the initialization
effectively takes place as part of the object allocation, not via the
execution of a constructor (i.e. the fields are already initialized by
the time any constructor is executed).
class Foo
{
int myInstanceInt;

public void MyMethod()
{
int myLocalInt;
Console.WriteLine(myLocalInt);
}
}

The local variable myLocalInt in method MyMethod is not automaticaly
initialized becuase if this valiable is not exlicit
initialized I get compile error when I try to write it's value as in this
statement Console.WriteLine(myLocalInt);

Also not technically true. What C# tells you is that the variable is
"not definitely assigned". As it happens, it actually is initialized,
at least for C# compiled for .NET. You're just not allowed to count on
it being initialized. C# requires that you _explicitly_ initialize a
variable before you use it.
I mean when instance value type like myInstanceInt above is automaticaly
initialized why is not
the local valiable myLocalInt in the method automaticaly initialized ?

As Arne says, you'd have to ask the language design team. My best guess
though is that it's a combination of factors:

– First and probably foremost is that it's impossible for the
compiler to know for sure whether a class field has been initialized
before use, because the field persists across method calls. On the
other hand, it's trivial for the compiler to know that a local variable
hasn't been initialized, and while it's much harder to know for sure it
_has_ been initialized, false positives on the compiler error are
uncommon and easy to deal with.

– Also, it is more useful, and less likely to lead to bugs, for an
object to be initialized with default values, and conversely less useful
and more likely to lead to bugs for a local variable to be used before
explicit initialization.

– While it's not great object design, it's much more plausible that
you'd have a class with at least some fields that remain uninitialized
for the lifetime of the object, without there ever being a problem with
that.

In fact, on the latter point, note that the compiler will in fact warn
you if you've got a class field that cannot be initialized or used, or
if you've got a local variable that has not been used. So it does what
it can, but there are limits.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top