- initializing VALUE TYPES to NULL or 0 is redundant since these types are initialized to 0 at creation time.
- Int32 i; <== already initialized to 0.
- multiple initializations for the same object
public class Test
{
private ArrayList t = new ArrayList();
public Test(){}
public Test(ArrayList l)
{
t = l;
}
}
{
private ArrayList t = new ArrayList();
public Test(){}
public Test(ArrayList l)
{
t = l;
}
}
- in this scenario, the ArrayList's initialization in the attribute is overriden by the initialization in the second constructor. It's like initializing the same variable twice. The following example is a better solution:
public class Test
{
private ArrayList t;
public Test()
{
Test(new ArrayList());
}
public Test(ArrayList l)
{
t = l;
}
}
{
private ArrayList t;
public Test()
{
Test(new ArrayList());
}
public Test(ArrayList l)
{
t = l;
}
}
- exception handling is impossible on attribute initialization. If exception handling is necessary, initialize the attribute in the constructor.
When initially instantiating a type, here's the order of operations:
- Static variable storage is set to 0
- Static variable initializers execute
- Static constructors for the base class execute
- Static constructor executes
- Instance variable is set to 0
- Instance variable is initialized
- Base class instance constructors execute (for a specific constructor overload)
- Instance constructor executes
No comments:
Post a Comment