Effective C# 学习笔记(十四) 尽量减少重复性的初始化逻辑
2011-07-05 22:55
441 查看
对于构造函数的复合调用,尽量少写,因为其会影响运行效率,因为运行的代码更多了。你可以这样调用。
//构造函数复合调用示例
public class MyClass
{
//
collection of data
private
List<ImportantData> coll;
//
Name of the instance:
private
string name;
public
MyClass() :
this(0,
"")
{
}
public
MyClass(int initialCount) :
this(initialCount,
string.Empty)
{
}
public
MyClass(int initialCount, string name)
{
coll
= (initialCount > 0) ?
new
List<ImportantData>(initialCount) :
new
List<ImportantData>();
this.name
= name;
}
}
//利用C#4.0新特性—参数默认值简化构造函数重载的复杂度,以减少重载过多带来的代码维护的工作量
public class MyClass
{
//
collection of data
private
List<ImportantData> coll;
//
Name of the instance:
private
string name;
//
Needed to satisfy the new() constraint.
public
MyClass() :
this(0,
string.Empty)
{
}
//注意这里的name参数用了"",而不是string.Empty作为默认值,这是由于string.Empty不是一个编译时的常量,所以不能作为默认参数值
public
MyClass(int initialCount = 0, string name = "")
{
coll
= (initialCount > 0) ?
new
List<ImportantData>(initialCount) :
new
List<ImportantData>();
this.name
= name;
}
}
注意:由于在继承中new()的约束,在父类中除了带默认参数的构造函数外,还要显示地声明一个无参的构造函数,如上代码所示。
//在C#3.0以前,可以使用this或base关键字来调用唯一实现逻辑的重载构造函数,而不会引起多次多层调用重载的构造函数,它也不会把实例变量的initializer拷贝到每个构造函数中。
// Not legal,
illustrates IL generated:
public class MyClass
{
private
List<ImportantData> coll;
private
string name;
public
MyClass()
{
//
No variable initializers here.
//
Call the third constructor, shown below.
this(0,
""); // Not legal, illustrative only.
}
public
MyClass(int initialCount)
{
//
No variable initializers here.
//
Call the third constructor, shown below.
this(initialCount,
"");
}
public
MyClass(int initialCount, string Name)
{
//
Instance Initializers would go here.
object();
// Not legal, illustrative only.
coll
= (initialCount > 0) ?
new
List<ImportantData>(initialCount) :
new
List<ImportantData>();
name
= Name;
}
}
对于静态成员变量,还要考虑到其只可初始化一次,所以不可在重载的构造函数中对静态成员变量赋值,哪怕只有一个唯一实现的构造函数。这时,只可利用隐式成员变量初始化方式初始化静态成员变量。
//构造函数复合调用示例
public class MyClass
{
//
collection of data
private
List<ImportantData> coll;
//
Name of the instance:
private
string name;
public
MyClass() :
this(0,
"")
{
}
public
MyClass(int initialCount) :
this(initialCount,
string.Empty)
{
}
public
MyClass(int initialCount, string name)
{
coll
= (initialCount > 0) ?
new
List<ImportantData>(initialCount) :
new
List<ImportantData>();
this.name
= name;
}
}
//利用C#4.0新特性—参数默认值简化构造函数重载的复杂度,以减少重载过多带来的代码维护的工作量
public class MyClass
{
//
collection of data
private
List<ImportantData> coll;
//
Name of the instance:
private
string name;
//
Needed to satisfy the new() constraint.
public
MyClass() :
this(0,
string.Empty)
{
}
//注意这里的name参数用了"",而不是string.Empty作为默认值,这是由于string.Empty不是一个编译时的常量,所以不能作为默认参数值
public
MyClass(int initialCount = 0, string name = "")
{
coll
= (initialCount > 0) ?
new
List<ImportantData>(initialCount) :
new
List<ImportantData>();
this.name
= name;
}
}
注意:由于在继承中new()的约束,在父类中除了带默认参数的构造函数外,还要显示地声明一个无参的构造函数,如上代码所示。
//在C#3.0以前,可以使用this或base关键字来调用唯一实现逻辑的重载构造函数,而不会引起多次多层调用重载的构造函数,它也不会把实例变量的initializer拷贝到每个构造函数中。
// Not legal,
illustrates IL generated:
public class MyClass
{
private
List<ImportantData> coll;
private
string name;
public
MyClass()
{
//
No variable initializers here.
//
Call the third constructor, shown below.
this(0,
""); // Not legal, illustrative only.
}
public
MyClass(int initialCount)
{
//
No variable initializers here.
//
Call the third constructor, shown below.
this(initialCount,
"");
}
public
MyClass(int initialCount, string Name)
{
//
Instance Initializers would go here.
object();
// Not legal, illustrative only.
coll
= (initialCount > 0) ?
new
List<ImportantData>(initialCount) :
new
List<ImportantData>();
name
= Name;
}
}
对于静态成员变量,还要考虑到其只可初始化一次,所以不可在重载的构造函数中对静态成员变量赋值,哪怕只有一个唯一实现的构造函数。这时,只可利用隐式成员变量初始化方式初始化静态成员变量。
相关文章推荐
- 《Effective C#》读书笔记——条目14:尽量减少重复的初始化逻辑<.NET资源管理>
- Effective C# 学习笔记(十) 用可选参数尽量减少方法重载
- Effective C# 学习笔记(十六)尽量少的创建对象,给GC减减负
- Effective C++学习笔记:尽量使用初始化而不要在构造函数里赋值
- Effective C# 学习笔记(十二) 多用成员变量初始化,少用指定赋值
- Effective C++学习笔记之“尽量使用初始化而不要在构造函数里赋值”
- Effective C# 学习笔记(十三)对静态类成员使用合适的初始化方式
- Effective C# 学习笔记(四十五)减少装箱拆箱行为
- Effective C# 学习笔记(十一)尽量缩减函数体的大小,提高运行效率
- nginx 源码学习笔记(十四)—— 全局变量ngx_cycle
- spring源码学习笔记-初始化(三)-BeanFactory
- unity3D-游戏/AR/VR在线就业班 C#入门关系运算和逻辑运算学习笔记
- iOS学习笔记-Objective-C-继承和实例化、对象初始化
- Windows 8 Directx 开发学习笔记(十四)使用几何着色器实现三角形细分
- Contiki学习笔记之时钟初始化
- [linux内存]伙伴系统学习笔记(二)--内存系统初始化
- Windows 8 Directx 开发学习笔记(二)建立模型及初始化设备
- ODAC(V9.5.15) 学习笔记(十四)TCRBatchMove
- MYSQL学习笔记(十四)使用全文本搜索
- 拦截器---SpringMVC学习笔记(十四)