2.为你的常量选择readonly而不是const

const 在编译时需要确定其值,所以const的字段默认是静态的,因而不能这样声明:static const string str;   因为const 是编译时确定,所以其不能用于带new的类型,只能用于一些简单类型。注const DateTime dt=new DateTime();是错误的。

readonly 是运行时常量,其值在编译时不确定。

posted on 2012-10-22 09:17  Q&A  阅读(115)  评论(0编辑  收藏  举报