C#中的const与static readonly区别
在许多教程中常看到列出了两者之间的区别
const
(1)在编译期间解析常量
(2)必须在申明时就初始化
(3)即可用来修饰类中的成员,也可用来修饰函数中的局部变量
static readonly
(1)在运行期间解析常量
(2)即可在申明时初始化,也可在构造器中初始化
(3)只可用来修饰类中的成员
从上面的区别看似乎也很明白,可是在实际的使用中还是不太好把握。在c#中有两种不同的常量:
静态常量(compile-time constants) 和动态常量(runtime constants),他们之间有不同的特性,错误的使用不仅会损失效率,
还可能造成错误。相比之下,静态常量在速度上会稍快写,但是灵活性却比动态常量差。
从我个人对他们的使用中总结出有如下三点需要注意:
(1)当使用new来初始化一个常量,不管他是值类型,还是引用类型都只能使用static readonly
1
static readonly MyClass myclass=new MyClass();2
//这样是错误的3
public const DateTime myDateTime=new DateTime(2007,7,1,1,1,1);4
//这样是正确的5
public static readonly DateTime myDateTime=new DateTime(2007,7,1,1,1,1);6

(2) 比较编译时和运行时他们之间的区别
1
static readonly A=B*20;
2
static readonly B=10;
3
//在编译的时候B并没有被编译为10,A也没有被编译为200
4
const A=B*20
5
const B=10
6
//显然在编译的时候,就把B编译为10,A编译为200了,而不是在运行时计算B*20
static readonly A=B*20;2
static readonly B=10;3
//在编译的时候B并没有被编译为10,A也没有被编译为2004
const A=B*205
const B=106
//显然在编译的时候,就把B编译为10,A编译为200了,而不是在运行时计算B*20
(3)在不同的项目中使用const和static readonly的区别
1
//下面有一个项目2
public class OneClass3
{4
public const int myInt=5;5
public static readonly string strStaticReadonly="StaticReadonly";6
}7
//在另一个项目中引用了上述属性8
public class AnotherClass9
{10
int i=OneClass.myInt;11
string ss=OneClass.strStaticReadonly;12
}13
//编译时运行程,i=5;ss="StaticReadonly",这是理所当然的,但是我们如过了OneClass中的值14
public class OneClass15
{16
public const int myInt=10;17
public static readonly string strStaticReadonly="Changed";18
}19
//然后编译OneClass项目,生成DLL,运行AnotherClass(只是运行,并不是编译后运行,发现结果是:20
//i=5不变,但是ss=“changed”,为什么会这样,原因是在AnotherClass中,i已经被定义为5了,而不是运行时再去取dll的值,所以说const在编译时就确定了。总结:
1 const是编译时常量,static readonly是运行时常量
2 效率优先,则用const(编译时赋值);安全优先,则static readonly(比如有a.dll和b.dll,a.dll中有一个const变量,b.dll引用了它,如果a.dll中的这个变量改变了值,但不重新编译b.dll就会造成问题了,而使用static readonly就不会有问题。)
3 readonly在声明时直接赋值只是语法糖,实际编译时这个赋值会放到constructor里面去。
4 readonly可在constructor里多次赋值,但是出了constructor就不能再改;const只能赋一次值。


浙公网安备 33010602011771号