Effective C#阅读笔记-5对于值类型保证0是一个有效状态
2011-08-07 11:25
267 查看
对于值类型默认没有赋值的情况下,默认值都是0。
枚举类型,继承于System.ValueType,默认值情况下枚举是从0开始的,但是可以修改
如果不是使用显示的指定枚举,用new分配一个枚举对象,sphere 目前的值类型默认值为0,此时sphere不再是一个有效的状态 ,其他引用的地方将会可能出错!可以使用下面的方法解决这个问题,为0指定一个为初始化的值
另外对于使用了Flags位标志属性的枚举,切记一定需要设置None值为0,确保0是一个有效的状态并且表示:“the absence of all flags”
You should use this property inside your own type. Doing so localizes the null reference check to one location. The
Message accessor is almost certainly inlined as well, when called from inside your assembly. You'll get efficient code and minimize errors.
The system initializes all instances of value typess to 0. There is no way to prevent users from creating instances of value types that are all
0s. If possible, make the all 0 case the natural default. As a special case, enums used as flags should ensure that
0 is the absence of all flags.
枚举类型,继承于System.ValueType,默认值情况下枚举是从0开始的,但是可以修改
public enum Planet { // Explicitly assign values. // Default starts at 0 otherwise. Mercury = 1, Venus = 2, Earth = 3, Mars = 4, Jupiter = 5, Saturn = 6, Neptune = 7, Uranus = 8, Pluto = 9 } Planet sphere = new Planet();
如果不是使用显示的指定枚举,用new分配一个枚举对象,sphere 目前的值类型默认值为0,此时sphere不再是一个有效的状态 ,其他引用的地方将会可能出错!可以使用下面的方法解决这个问题,为0指定一个为初始化的值
public enum Planet { None = 0, Mercury = 1, Venus = 2, Earth = 3, Mars = 4, Jupiter = 5, Saturn = 6, Neptune = 7, Uranus = 8, Pluto = 9 } Planet sphere = new Planet();
另外对于使用了Flags位标志属性的枚举,切记一定需要设置None值为0,确保0是一个有效的状态并且表示:“the absence of all flags”
[Flags] public enum Styles { None = 0, Flat = 1, Sunken = 2, Raised = 4, }
字符串默认值为null,可以通过属性来对外公开字符串的访问,通过get做检查,如果为null返回String.Empty字符串,类型内部检查。 public struct LogMessage { private int _ErrLevel; private string _msg; public string Message { get { return (_msg != null ) ? _msg : string.Empty; } set { _msg = value; } } }
You should use this property inside your own type. Doing so localizes the null reference check to one location. The
Message accessor is almost certainly inlined as well, when called from inside your assembly. You'll get efficient code and minimize errors.
The system initializes all instances of value typess to 0. There is no way to prevent users from creating instances of value types that are all
0s. If possible, make the all 0 case the natural default. As a special case, enums used as flags should ensure that
0 is the absence of all flags.
相关文章推荐
- 《Effective C#》读书笔记——条目19:保证0为值类型的有效状态<.NET资源管理>
- Effective C# 学习笔记(十九)确保0(null)对于值(引用)类型是一个合法(理)的默认值
- 《Effective C#》条款8:确保0为值类型的有效状态
- Effective C#阅读笔记-6Prefer Immutable Atomic Value Types不可变值类型
- 保证0为值类型的有效状态<.NET资源管理>
- Effective C# 原则8:确保0对于值类型数据是有效的
- 《Effective C#》条款8:确保0为值类型的有效状态
- Effective C# 原则8:确保0对于值类型数据是有效的(翻译)
- Effective C#阅读笔记-3使用is或者as代替cast做类型转换
- 《Effective C#》Item 8:确保“0”在值类型中是有效的
- “ 一个程序员的Java和C++学习之路”阅读笔记2
- 有效需求分析阅读笔记三
- C++ primer第二次阅读学习笔记(第18章:特殊工具与技术:运行时类型识别、extern。。。)
- openerp学习笔记 视图样式(表格行颜色、按钮,字段只读、隐藏,按钮状态、类型、图标、权限,group边距,聚合[合计、平均],样式)
- Chun 阅读笔记 - 如何做一个图片缓存库
- Effective C# Item8:确保0是值类型的有效状态
- 编写有效用例_阅读笔记02
- 编写有效用例_阅读笔记04
- Effective C# 学习笔记(三)在类型转换上多使用 as 和 is
- Effective C#阅读笔记-4总是为你的类提供ToString()方法