Readonly and Mutable, vs. Read / Write Properties
2005-11-20 18:13
423 查看
刚刚逛Bill Wanger的blog看到了一篇帖子,虽然感觉没什么新意,不过既然他老人家郑重其事地提出来了,估计有他的用意,自己写了个程序验证了一下,贴在下面,由于他老人家用的是C#2.0(用到了范型),俺机子没有装,只能把它翻译成C#1.1的版本了.
class ClassPrint
class ClassWords
private ArrayList words = new ArrayList();
public ArrayList _pWords
private ArrayList words = new ArrayList();
public ArrayList _pWords
{
get
{
return words;
}
set
{
words = value;
}
}
却可以让它指向另一个ArrayList,这样如果一个客户端代码已经缓存了这个属性原始数据的引用,如果用set方法让它指向新的ArrayList就会显得很突然和奇怪.
我觉得这其实是很正常和比较好理解的,如果类在设计的时候不允许它再指向新的ArrayList(当然这里的Mutable,可以指任何引用类型的数据,比如DataSet等等),那就把这个字段设计为只读,如果所有的实例只允许一份值拷贝,干脆用Sington Pattern.
Bill Wanger写的<<Effective C#>>和<<The C# Core Language Little Black Book>>据说很经典,等看完Anders就可以看这两本了,爽.
class ClassPrint
class ClassWords
private ArrayList words = new ArrayList();
public ArrayList _pWords
private ArrayList words = new ArrayList();
public ArrayList _pWords
{
get
{
return words;
}
set
{
words = value;
}
}
却可以让它指向另一个ArrayList,这样如果一个客户端代码已经缓存了这个属性原始数据的引用,如果用set方法让它指向新的ArrayList就会显得很突然和奇怪.
我觉得这其实是很正常和比较好理解的,如果类在设计的时候不允许它再指向新的ArrayList(当然这里的Mutable,可以指任何引用类型的数据,比如DataSet等等),那就把这个字段设计为只读,如果所有的实例只允许一份值拷贝,干脆用Sington Pattern.
Bill Wanger写的<<Effective C#>>和<<The C# Core Language Little Black Book>>据说很经典,等看完Anders就可以看这两本了,爽.
相关文章推荐
- [zz] Read and write a properties file
- [TypeScript] Make Properties and Index Signatures Readonly in TypeScript
- How to use java Properties API -- read and write
- Database 'xxx' cannot be upgraded because it is read-only or has read-only file Make the database or files writeable, and rerun recovery.
- [TS] Class Properties Public, Private and Read Only Modifiers
- sysbench write and read only
- java.lang.UnsupportedOperationException: Can't write to a readonly object
- python.pandas read and write CSV file
- const VS readonly in detail
- Spring+Hibernate报错:Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL)
- Write operations are not allowed in read-only
- SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)
- Freemaker的java.beans.IntrospectionException: type mismatch between read and write methods
- 关键字(nonatomic/atomic)、(copy/retain/assign/strong/weak )、(readonly / readwrite)、(@property/@
- MapInfo .TAB and .MIF/.MID Read/Write Library
- Dual-Port Block RAM with Two Write Ports and Byte-wide Write Enable in Read-First Mode
- Investigation report of MSSQL data read and write performance
- Copy files to a folder which need have Administrator approve and overwrite the existing same readonly files
- firebase database read and write
- Objective-C中的@property和@synthesize用法及参数(readwrite/readonly)(assign/retain/copy)(atomicity/nonatomic)