首页 > 代码库 > const常量和readyonly常量区别

const常量和readyonly常量区别

1、const常量为静态常量;readyonly常量为动态常量;
2、const常量在编译时值被确定,在运行时值为编译时值;readyonly常量,在编译时为类型的默认值(非指定的默认值),在运行时值被确定;
3、const常量无内存消耗;readyonly存常量有内存消耗;
4、const常量为引用类型,值为string.Empty或null;readyonly为值类型,可以为任何值;

如: const int A=1; readyonly int B=1; ------------------- 编译时:A=1;B=0; 运行时:A=1;B=1; A=1; B=1; ------------------- const int A=B+1; readyonly int B=1; ------------------- 编译时:B=0;A=0+1=1; 运行时:A=1;B=1; A=1; B=1; ------------------- const int A=B+1const int B=1; ------------------- 编译时:B=1;A=1+1=2; 运行时:A=2;B=1; A=2; B=1; -------------------

 

const常量和readyonly常量区别