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