Item 12:Perfer Variable Initializers to Assignment Statements
2005-08-12 18:53
330 查看
编译器总是在运行Constructor前,先初始化成员变量(不包括静态变量)。当有初始化语句时,你不需要为每个构造函数添加初始化语句。Equally important,the initializers are added to the complier-generated default constructor.The C# complier creates a default constructor for you types whenever you don't explicitly define any constructors.
注意:有三种情况不适合使用initializer
1.当对象或变量被初始化为null或0时
MyValueType _myclass; //initicalized to 0
MyValueType _myclass = new MyValueType(); //also 0
The default system initialization sets everything to 0(null) for you before any of your code executes.
如果你添加了额外的初始化语句(如第二种)C#编译器必需添加额外的语句去把变量重新初始化为0
2.You should use the initializer syntax only for variables that receive the same initialization in all constructor.
public class MyClass
public class MyClass
private ArrayList _col;
MyClass()
_col = new ArrayList();
}
MyClass(int size)
_col = new ArrayList();
_col = new ArrayList(size);
}
}
[b]3.如果一个成员变量初始化时很容易出错,就尽量不要使用initialization。 [/b]
注意:有三种情况不适合使用initializer
1.当对象或变量被初始化为null或0时
MyValueType _myclass; //initicalized to 0
MyValueType _myclass = new MyValueType(); //also 0
The default system initialization sets everything to 0(null) for you before any of your code executes.
如果你添加了额外的初始化语句(如第二种)C#编译器必需添加额外的语句去把变量重新初始化为0
2.You should use the initializer syntax only for variables that receive the same initialization in all constructor.
public class MyClass
public class MyClass
private ArrayList _col;
MyClass()
_col = new ArrayList();
}
MyClass(int size)
_col = new ArrayList();
_col = new ArrayList(size);
}
}
[b]3.如果一个成员变量初始化时很容易出错,就尽量不要使用initialization。 [/b]
相关文章推荐
- Item 12: Prefer Member Initializers to Assignment Statements(Effective C#)
- Effective C#之12:Prefer Variable Initializers to Assignment Statements
- Effective C# Item12: Prefer Variable Initializers to Assignment Statements
- Item 12. 赋值和初始化(Assignment and Initialization Are Different)
- Effective C++, 3rd Edition, Item 11: 在 operator= 中处理 assignment to self(自赋值)
- Item 12. 赋值和初始化(Assignment and Initialization Are Different)
- [翻译] Effective C++, 3rd Edition, Item 11: 在 operator= 中处理 assignment to self(自赋值)
- Item21 Perfer std::make_unique and std::make_shared to direct use of new
- theano 中出现 TypeError: 'TensorVariable' object does not support item assignment
- Item 12. 赋值和初始化(Assignment and Initialization Are Different)
- Item 11: Handle assignment to self in operator=(Effective C++)
- nodejs Assignment to constant variable
- EffectiveJava(Item: 1 to 12)
- Item10: Have assignment operators return a reference to *this
- Flex中TypeError: Error #1089: Assignment to lists with more than one item is not supported.
- Swift 提示:Initialization of variable was never used consider replacing with assignment to _ or removing it
- Effective C++ Item 10,11 Have assignment operators return a reference to *this Handle assignment to self in operator =
- Item 10: Have assignment operators return a reference to *this(Effective C++)
- [EffectiveC++]item10:have assignment operators return a reference to *this
- Initialization of variable was never used; consider replacing with assignment to ‘_’ or removing it