Re: Should I explicitly initialize all member variables?
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Sun, 14 Aug 2005 19:36:11 +0100
Jeff Louie <jeff_louie@xxxxxxxxx> wrote:
> Javamman.. The following two lines of code are NOT equivalent in C#.
>
> >MyClass m = null; // redundant
> MyClass m; // better?<
>
> The second line simply creates an unitialized variable. It does not
> default to null.
Yes it does - if it's a member variable. If it's a *local* variable it
remains uninitialised, but the subject line makes it clear that the
question is about member variables. From the spec, section 12.2 (ECMA
numbering):
<quote>
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.1).
* For a variable of a reference-type, the default value is null.
</quote>
> http://www.geocities.com/Jeff_Louie/OOP/oop5.htm
There are a couple of slight errors in that, in the Java bit:
4) Value types *don't* actually derive from anything - only the
reference type which is used for boxing the value type derives
(indirectly) from Object. It's all a bit odd, but is spelled out in the
CLI spec.
9) Java has enums now too - they're very different to C# ones though
(and far superior, IMO).
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.
- Follow-Ups:
- Re: Should I explicitly initialize all member variables?
- From: Jeff Louie
- Re: Should I explicitly initialize all member variables?
- References:
- Should I explicitly initialize all member variables?
- From: Javaman59
- Re: Should I explicitly initialize all member variables?
- From: Jeff Louie
- Should I explicitly initialize all member variables?
- Prev by Date: Windows Services
- Next by Date: Re: How can I tell the application total memory usage? (LONG!)
- Previous by thread: Re: Should I explicitly initialize all member variables?
- Next by thread: Re: Should I explicitly initialize all member variables?
- Index(es):
Relevant Pages
|