const常量和readonly常量区别

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

如:
 const int A=1;
 readonly int B=1;
 -------------------
 编译时:A=1;B=0;
 运行时:A=1;B=1;
 A=1;
 B=1;
 -------------------
 const int A=B+1;
 readonly 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;
 -------------------
原文地址:https://www.cnblogs.com/raowenbin/p/4060124.html