By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,446 Members | 3,090 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,446 IT Pros & Developers. It's quick & easy.

Default value of basic variable type

P: n/a
i am a newbie,
i remember i read a book talking about when u declare a array variable
using
float[] ABC = new float[10];

the whole array element in ABC ( ABC[0] to ABC[9] ) will automatic
initialize to 0
and all type of numeric variable is initialize to 0, bool type is false,

is it true?

i can't find the book now, can u please telling me?

THANKS
Aug 16 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Macneed wrote:
i am a newbie,
i remember i read a book talking about when u declare a array variable
using
float[] ABC = new float[10];

the whole array element in ABC ( ABC[0] to ABC[9] ) will automatic
initialize to 0
and all type of numeric variable is initialize to 0, bool type is false,

is it true?
In an instance of a class, yes. So all the floats in a "new float[10]"
get initialized to the default value for the float (0). Likewise, all
the bools in a "new bool[10]" get initialized to false.

Note, however, that a stack variable is not initialized. You're
required to initialize it explicitly before you use it. The compiler
generates an error if you attempt to use the variable before you
initialize it (and in many cases, even if you do, if you're not clear
enough to suit the compiler :) ).

Pete
Aug 16 '07 #2

P: n/a
Yes and no.

all value types have a devault constructor that initalizes the value
to 0.
See: http://download.microsoft.com/downlo...cification.doc
Section: 4.1.2

But be carefull these defaultconstructors are not called when
declaring a local variable.

Sample:

class A
{
int a;
public A()
{
Console.WriteLine(a);
}

//compile time error use of unassigned local variable b
//private void DoWork()
//{
// int b;
// Console.WriteLine(b);
//}
}

Aug 16 '07 #3

P: n/a
Yes
This is part of the language spec (ECMA-334, 3rd edition):

19.2 Array creation
Elements of arrays created by array-creation-expressions are always
initialized to their default value
(12.2).
12.2 Default values
* 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.
11.1.2 Default constructors
All value types implicitly declare a public parameterless instance
constructor called the default constructor.
The default constructor returns a zero-initialized instance known as
the default value for the value type:
* For all simple-types, the default value is the value produced by a
bit pattern of all zeros:
* For sbyte, byte, short, ushort, int, uint, long, and ulong, the
default value is 0.
* For char, the default value is '\x0000'.
* For float, the default value is 0.0f.
* For double, the default value is 0.0d.
* For decimal, the default value is 0m.
* For bool, the default value is false.
* For an enum-type E, the default value is 0.
* For a struct-type, the default value is the value produced by
setting all value type fields to their default
value and all reference type fields to null.
Aug 16 '07 #4

P: n/a
Using fxcop rules dealt on that.

The rule name is : "Do not initialize unnecessarily"
================================

Rule Description :

Do not initialize unnecessarily
TypeName: DoNotInitializeUnnecessarily
CheckId: CA1805
Category: Microsoft.Performance
Message Level: Warning
Certainty: 90%
Breaking Change: NonBreaking

Cause: A static or instance constructor initializes a field to its default
value. This rule ignores Managed C++ assemblies.

Rule Description
The common language runtime initializes all fields to their default values
before running the constructor. In most cases, initializing a field to its
default value in a constructor is redundant, which degrades performance and
adds to maintenance costs. One case where it is not redundant occurs when the
constructor calls another constructor of the same class or a base class
constructor and that constructor initializes the field to a non-default
value. In this case, changing the value of the field back to its default
value can be appropriate.

How to Fix Violations
To fix a violation of this rule, remove the field initialization from the
constructor. Note that the C# compiler that is included with .NET Framework
version 2.0 removes these unnecessary initializations when the optimize
option is enabled.

When to Exclude Messages
Exclude a message from this rule if the constructor calls another
constructor in the same or base class that initializes the field to a
non-default value. It is also safe to exclude a message from this rule, or
disable the rule entirely, if performance and code maintenance are not
priorities.

Example Code
The following example shows a type that contains multiple violations of the
rule.

[C#]

using System;

namespace PerformanceLibrary
{
class InitializeUnnecessarily
{
bool b1;
bool b2;
int i;
double d;
string s;

InitializeUnnecessarily()
{
b1 = true;

// The following field assignments are violations of this rule.
b2 = false;
i = 0;
d = 0;
s = null;
}

InitializeUnnecessarily(string s) : this()
{
// Exclude the warning for the following statement.
b1 = false;

this.s = s;
}
}
}

[Visual Basic]

Imports System

Namespace PerformanceLibrary

Class InitializeUnnecessarily

Dim b1 As Boolean
Dim b2 As Boolean
Dim i As Integer
Dim d As Double
Dim s As String

Sub New()

b1 = True

' The following field assignments are violations of this rule.
b2 = False
i = 0
d = 0
s = Nothing

End Sub

Sub New(s As String)

Me.New()

' Exclude the warning for the following statement.
b1 = False

Me.s = s

End Sub

End Class

End Namespace
--
Sincerely
Yaron Karni
http://dotnetbible.blogspot.com/
"Macneed" wrote:
i am a newbie,
i remember i read a book talking about when u declare a array variable
using
float[] ABC = new float[10];

the whole array element in ABC ( ABC[0] to ABC[9] ) will automatic
initialize to 0
and all type of numeric variable is initialize to 0, bool type is false,

is it true?

i can't find the book now, can u please telling me?

THANKS
Aug 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.